您的位置:首页 > Web前端 > JavaScript

JSP (1)

2015-06-07 05:06 671 查看
1. HTTP方法

GET : 像特定的资源发出请求
POST: 向指定资源提交数据进行处理请求

2 用JSP输出hello

用myeclipse创建java web项目,在如图:




在Hello中添加代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!--html <%=new java.util.Date().toString() %> -->
<%--JSP <%=new java.util.Date().toString() %> --%>
<%!
String str = "Hello"; // 全局变量
%>
<%
out.println(str);
%>
</body>
</html>


编译指令

Page指令

用于定义当前jsp程序的全局属性,位于最上方
<%@ page language="java" // 声明当前jsp使用的脚本语言的种类
contentType="text/html; charset=ISO-8859-1" // 服务器生成网页的编码
pageEncoding="ISO-8859-1" // 用于设置jsp本身的编码
%>
import用于导入java类
<%@ page import = "java.util.ArrayList" %>
<%@ page import ="java.util.LinkedList" %>

include 指令

将一个外部文件包含到当前的jsp程序中
<body>
<%@ include file="header.html" %>
<br/>
<%@ include file="Hello.jsp" %>
</body>


动作指令

forward指令

是一个重定向指令,forward下面的代码不会被执行
<jsp:forward page="myinclude.jsp"></jsp:forward>

传递参数:

<jsp:forward page="myinclude.jsp">
<jsp:param value="thystar" name="userName"/>
<jsp:param value="123" name="password"/>
</jsp:forward>


myinclude.jsp中:
<body>
<%
String userName = request.getParameter("userName");
String password = request.getParameter("password");
out.println(userName);
out.println("</br>");
out.println(password);
%>
</body>


include指令: 动态包含指令

动态:jsp
静态:html
<jsp:include page="myinclude.jsp">
<jsp:param value="red" name="bjcolor"/>
</jsp:include>
myinclude.jsp中
<body bgcolor="<%=request.getParameter("bjcolor")%>">

</body>


useBean指令
添加类





添加代码:

package com.jike.entity;

import java.io.Serializable;

public class UserEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String userName;
private String password;
public UserEntity() {
super();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}


新建register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>录入注册信息</title>
</head>
<body>
<form action="doregister.jsp" method="post">
用户名: <input type="text" name="userName"/>
密码:<input type="password" name="password"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>


新建doregister.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>提交的内容</title>
</head>
<body>
<jsp:useBean id="user" class="com.jike.entity.UserEntity"></jsp:useBean>
<jsp:setProperty property="userName" name="user"/>
<jsp:setProperty property="password" name="user"/>

<jsp:getProperty property="userName" name="user"/>
<jsp:getProperty property="password" name="user"/>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java Web