Servlet续
JSP
-
JSP介绍: JSP全称Java Server Pages,是一种动态网页开发技术。它使用JSP标签在HTML网页中插入Java代码。标签通常以<%开头以%>结束。
-
JSP是一种java servlet,主要用于实现Java web应用程序的用户界面部分。网页开发者们通过结合HTML代码、XHTML代码、XML元素以及嵌入JSP操作和命令来编写JSP。
-
JSP通过网页表单获取用户输入数据、访问数据库及其他数据源,然后动态地创建网页。
-
JSP标签有多种功能,比如访问数据库、记录用户选择信息、访问JavaBeans组件等,还可以在不同的网页中传递控制信息和共享信息。
JSP生命周期
- 编译阶段:
- servlet容器编译servlet源文件,生成servlet类
- 初始化阶段:
- 加载与JSP对应的servlet类,创建其实例,并调用它的初始化方法
- 执行阶段:
- 销毁阶段:
- 调用与JSP对应的servlet实例的销毁方法,然后销毁servlet实例
public void Init(){
}
void _jspService(HttpServletRequest request,
HttpServletResponse response)
{
}
public void Destroy()
{
}
JSP是一种脚本语言:
- 脚本程序可以包含任意量的Java语句、变量、方法或表达式,只要它们在脚本语言中是有效的。
<% int num=5 %>
<%= num %>
examp:
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
<%
String str="欢迎来到小M开发";
out.println(str);
%>
</body>
</html>
运行结果(tomcat服务器上运行):

JSP 页面重定向
- 当需要将文档移动到一个新的位置时,就需要使用JSP重定向了。
最简单的重定向方式就是使用response对象的sendRedirect()方法
package com.yy.servlet.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yy.servlet.dao.UserDao;
import com.yy.servlet.po.User;
public class ChangeServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String username=request.getParameter("username");
String userpwd=request.getParameter("userpwd");
username=new String(username.getBytes("ISO-8859-1"), "UTF-8");
userpwd=new String(userpwd.getBytes("ISO-8859-1"), "UTF-8");
User user=UserDao.getInstance().isLogin(username, userpwd);
if(user!=null){
response.sendRedirect("/ServletForWord/successful.jsp");
return;
}else{
/**网页重定项 url会变成对应的url*/
response.sendRedirect("http://www.baidu.com");
return;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
<%@ 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">
<script>
var password=0;
function showName(){
var name=document.getElementById("name").value;
if(name==""){
document.getElementById("nameFont").innerHTML="<img src ='image/wrong.jpg' style='margin-top:0px;'/><div style='color:red;margin-top:-19px;font-size:10px;margin-left:30px;'>你输入的名字为空</div>";
}else{
document.getElementById("nameFont").innerHTML="<img src ='image/right.jpg'/>";
}
}
function showPwd(){
var pwd=document.getElementById("pwd").value;
if(pwd==""){
document.getElementById("pwdFont").innerHTML="<img src ='image/wrong.jpg' style='margin-top:0px;'/><div style='color:red;margin-top:-19px;font-size:10px;margin-left:30px;'>你输入的密码为空</div>";
}else if(pwd.lenght>8&&pwd.lenght<16){
document.getElementById("pwdFont").innerHTML="<img src ='image/wrong.jpg' style='margin-top:0px;'/><div style='color:red;margin-top:-19px;font-size:10px;margin-left:30px;'>密码需要在8-16位</div>";
}else{
document.getElementById("pwdFont").innerHTML="<img src ='image/right.jpg'/>";
}
}
</script>
</head>
<body>
<center>
<h1 style ="color:red;font-size:60px">用户登录</h1>
</center>
<hr></hr>
<form action="change" method="get">
<table cellpadding="5">
<tr>
<td><span>NAME:</span></td>
<td ><input style="width:200px" type="text" value="youe name" name="username" id="name" onfocus="showName()" onblur="showName()"/></td>
<td id="nameFont"></td>
</tr>
<td><span>PWD:</span></td>
<td><input style="width:200px" type="password" value="888" name="userpwd" id="pwd" onfocus="showPwd()" onblur="showPwd()"/></td>
<td id="pwdFont"></td>
</tr>
<tr>
<td>
<input type="submit" value="登录" />
</td>
<td>
</td>
</tr>
</table>
<hr></hr>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.yy.servlet.web.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ChangeServlet</servlet-name>
<servlet-class>com.yy.servlet.web.servlet.ChangeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ChangeServlet</servlet-name>
<url-pattern>/change</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>fail.jsp</welcome-file>
</welcome-file-list>
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
/*这个是successful.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">
</head>
<body>
<span style="font-size:55px;color:red">${username} 登录成功!</span>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
运行结果(tomcat服务器上运行):
login_test.jsp

登陆成功

登录失败(跳转到百度)

request.getRequestDispatcher(“url”).forward(request, response)跳转
现在我们把ChangeServlet类换成LoginServlet ,把login_test.jsp的action=”login” ,successful.jsp不变
package com.yy.servlet.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yy.servlet.dao.UserDao;
import com.yy.servlet.po.User;
public class LoginServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
String username=request.getParameter("username");
String userpwd=request.getParameter("userpwd");
username=new String(username.getBytes("ISO-8859-1"), "UTF-8");
userpwd=new String(userpwd.getBytes("ISO-8859-1"), "UTF-8");
User user=UserDao.getInstance().isLogin(username, userpwd);
if(user!=null){
request.setAttribute("username", username);
request.getRequestDispatcher("/successful.jsp").forward(request, response);
return;
}else{
/**指定跳转到regist.jsp的页面(内部跳转,不能跳转到其他外部网址)*/
request.setAttribute("username", username);
request.getRequestDispatcher("/fail.jsp").forward(request, response);
return;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
/*这里我们在加一个fail.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">
</head>
<body>
<span style="font-size:55px;color:red">${username}登录失败!</span>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
运行结果(tomcat服务器上运行):
login_test.jsp

登录成功

登录失败

注意:
- response.sendRedirect(“url”):url会变成对应的跳转去的url,可以跳转到项目外的网页
- request.getRequestDispatcher(“url”).forward(request, response):指定跳转到某个的页面,但是url会显示为跳转页面的Servlet类url(内部跳转,不能跳转到其他外部网址)