信息流作用方向:index.jsp(注册界面)→Servlet.java(接收到用户名密码并且储存在javaBean中)→将注册成功的用户保存在session中→在deal.jsp获取注册成功的用户的信息(之后销毁session)
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
errorPage="error.jsp"%>
<%
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 'error.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>
<%
//销毁session
session.invalidate();
%>
<body>
<form method="get" action="itcast/Servlet" >
姓名: <input type="text" name="userName" ><br>
密码: <input type="password" name="passWord"><br>
<input type="submit" value="登陆">
<input type="reset" value="重置">
</form>
</body>
</html>
deal.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@page import="itcast.UserBean"%>
<html>
<head>
<title>$Title$</title>
</head>
<body><pre>
注册成功
<jsp:useBean id="loginUser" class="itcast.UserBean" scope="session"></jsp:useBean>
用户名:<%=loginUser.getUserName() %>
密码:<%=loginUser.getPassWord() %>
<%
//销毁session
session.invalidate();
%>
</pre>
</body>
</html>
error.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 'error.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>
<%response.setHeader("refresh","1;URL=index.jsp"); %>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Servlet</servlet-name>
<servlet-class>itcast.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/itcast/Servlet</url-pattern>
</servlet-mapping>
</web-app>
Servlet.java
package itcast;
import java.io.IOException;
import java.io.PrintWriter;
import itcast.UserBean;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
/**
* Constructor of the object.
*/
public Servlet() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
String us = request.getParameter("userName");
String ps = request.getParameter("passWord");
CharactorEncoding ch = new CharactorEncoding();
String us1 = ch.toString(us); //防止中文乱码
System.out.println(us1+";"+ps);
if(us1.equals("张三") && ps.equals("abc")){
UserBean user = new UserBean();
user.setUserName(us1);
user.setPassWord(ps);
request.getSession().setAttribute("loginUser", user);//注册成功的用户保存在session中
response.sendRedirect("/Testttt/deal.jsp");
}else{
response.getWriter().write("请输入正确的账户密码");
}
response.getWriter().append("Served at: ").append(request.getRequestURI());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
UserBean.java
package itcast;
public class UserBean {
private String userName;
private String passWord;
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;
}
}
CharactorEncoding.java
package itcast;
import java.io.UnsupportedEncodingException;
public class CharactorEncoding {
public CharactorEncoding() {
}
/**
* 对字符串进行处理
*
* @param str要转码的字符串
* @return 编码后的字符串
*/
public String toString(String str) {
String text = "";
if (str != null && !"".equals(str)) {
try {
text = new String(str.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return text;
}
}