版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
1. include指令
1. include.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'include.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<p>插入txt文件:<%@include file="/include-txt.txt"%></p>
<p>插入HTML文件:<%@include file="/include-html.html"%></p>
<p>插入jsp文件:<%@include file="/include_jsp.jsp"%></p>
<p>插入code文件:<%@include file="/include_code.code"%></p>
<%
//当文件找不到路径的时候, 注意观察文件的相对路径的上下级关系
//一个页面里面可以包含多个页面 @inclue file="/include-html.html"
%>
</body>
</html>
2.include-txt.txt
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%="这是插入的文本文件"%>
3.include-html.html
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<font color=blue size=4>
这是插入的HtML文档
</font>
4.include_jsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.util.*"%>
<%=new Date().toString()%>
<%="这是插入的JSP文件的内容"%>
5.include_code.code
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%String s="这是插入执行代码的内容";
out.print(s);
%>
2. include动作
1.login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form method=get action=checklogin.jsp>
<table>
<tr>
<td>请输入用户名:</td>
<td><input type=text name=name
value=<%=request.getParameter("user")%>></td>
</tr>
<tr>
<td>输入密码:</td>
<td><input type=password name=password></td>
</tr>
<tr colspan=2>
<td><input type=submit value=login></td>
</tr>
</table>
</form>
</body>
</html>
2.checklogin.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'checklogin.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%--进行登录检查--%>
<%
out.println("登录成功");
String name=request.getParameter("name");//request.getParameter(" ")通过此方法获取字符串
String password=request.getParameter("password");
//要对name和password可能没有获取值而为null,所以要进行预处理
if(name!=null && password!=null)
{
if(name.equals("123") && password.equals("123")){
%>
<jsp:forward page="success.jsp">
<jsp:param name="user" value="<%=name%>"/>
</jsp:forward>
<%
//错误原因:使用的网页不存在导致无法发生转跳
out.println("登录成功");
}
else{
%>
<jsp:forward page="login.jsp">
<jsp:param name="user" value="<%=name%>"/>
</jsp:forward>
<%
}
}
%>
</body>
</html>
3.success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
登陆成功
<br>
欢迎你,
<%=request.getParameter("user") %>
</body>
</html>
3.usebean和getProperty动作
在ch5的src文件夹的default创建TestBean.java文件
package default1;
public class TestBean{
public String userName;
public String password;
public int age;
public String getUserName() { //命名规则,首字母不能大写:get的g是小写
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
在工程ch5的WebRoot中创建两个文件
1. register.html
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'register.html' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<br>
<hr>
<form method="get" action="register.jsp">
<table>
<tr>
<td>姓名:<input name="userName" type="text"></td>
</tr>
<tr>
<td>密码:<input name="password" type="password"></td>
</tr>
<tr>
<td>年龄:<input name="age" type="text"></td>
</tr>
<tr>
<td><input type=submit value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
2.register.jsp
1.注意导入TestBean文件
<jsp:useBean id=“user” scope=“page” class=“default1.TestBean”/>
2.注意函数的使用要与TestBean一致
<%=user.getUserName()%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:useBean id="user" scope="page" class="default1.TestBean"/>
<jsp:setProperty name="user" property="*"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'register.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
注册成功:
<br>
<hr>
使用bean属性方法:
<br>
用户名:
<%=user.getUserName()%>
<br>
密码:
<%=user.getPassword() %>
<br>
年龄:
<%=user.getAge() %>
<br>
<hr>
使用getProperty:
<br>
用户名:
<jsp:getProperty name="user" property="userName"/>
<br>
密码:
<jsp:getProperty name="user" property="password"/>
<br>
年龄:
<jsp:getProperty name="user" property="age"/>
<br>
</body>
</html>
4.常见错误
1./include.jsp(行:27,列:17)/include-html.html(行:1,列:2)未终止&lt;%@页面标记
不能使用html格式对页面解析
应该采用jsp格式方式对页面解析
<%@ page language=“java” import=“java.util.*” pageEncoding=“gbk”%>
2.跳转失败
3.跳转到包含文件出现乱码
解决方法:将utf格式换成gbk格式
更改前:
更改后:
<%@ page language=“java” import=“java.util.*” pageEncoding=“gbk”%>
3