1、程序的结构流程图:
1、User.jsp(javabean)
package com.javabean;
public class User {
private String username;
private String password;
private String quanxian;
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;
}
public String getQuanxian() {
return quanxian;
}
public void setQuanxian(String quanxian) {
this.quanxian = quanxian;
}
}
2、UserAdmin.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 'MyJsp.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>
<%String value = (String)request.getAttribute("quanxian"); %>
<form action="UserAdminServlet" method = "post">
username :<input type = "text" name = "username" value = "<%= (null == request.getAttribute("username"))? "" : request.getAttribute("username") %>"><br>
password :<input type = "password" name = "password"><br>
权限:<select name = "quanxian">
<!-- 获取的权限值收费符合 否则为空 -->
<option value = "commom" <%= "commom".equals(value) ? "selected" : "" %>>普通用户</option>
<option value = "admin" <%= "admin".equals(value) ? "selected" : "" %>>管理员</option>
</select><br>
<input type = "submit" value = "提交">
</form>
</body>
</html>
<%--
//下面是重定向传来获得参数对比
<form action="UserAdminServlet2" method = "post">
username :<input type = "text" name = "username" value = "<%= (null == request.getParameter("username"))? "" : request.getParameter("username") %>"><br>
password :<input type = "password" name = "password"><br>
权限:<select name = "quanxian">
<option value = "commom" <%= "commom".equals(request.getParameter("quanxian")) ? "selected" : "" %>>普通用户</option>
<option value = "admin" <%= "admin".equals(request.getParameter("quanxian")) ? "selected" : "" %>>管理员</option>
</select><br>
<input type = "submit" value = "提交">
</form>
--%>
3、UserAdminServlet
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.javabean.User;
public class UserAdminServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String quanxian = request.getParameter("quanxian");
//是否为普通用户?session/UserAdminSuccess.jsp :session/UserAdmin.jsp;
if ("commom".equals(quanxian)) {
if ("commom".equals(username) && "commom".equals(password)) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setQuanxian(quanxian);
HttpSession session = request.getSession();
session.setAttribute("user", user);
request.getRequestDispatcher("session/UserAdminSuccess.jsp").forward(
request, response);
} else {
request.setAttribute("username", username);
request.setAttribute("quanxian", quanxian);
//下面重定向是通过地址栏的方式传递参数
//response.sendRedirect("UserAdmin2.jsp?username="+username+"&quanxian="+quanxian+"");
request.getRequestDispatcher("session/UserAdmin.jsp").forward(request,
response);
}
//是否为admin用户 ? session/UserAdminSuccess.jsp :session/UserAdmin.jsp;
} else if ("admin".equals(quanxian)) {
if ("admin".equals(username) && "admin".equals(password)) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setQuanxian(quanxian);
HttpSession session = request.getSession();
session.setAttribute("user", user);
//下面重定向是通过地址栏的方式传递参数
//response.sendRedirect("UserAdmin2.jsp?username="+username+"&quanxian="+quanxian+"");
request.getRequestDispatcher("session/UserAdminSuccess.jsp").forward(
request, response);
} else {
request.setAttribute("username", username);
request.setAttribute("quanxian", quanxian);
request.getRequestDispatcher("session/UserAdmin.jsp").forward(request,
response);
}
} else if ("".equals(username) || "".equals(password)) {
//下面重定向是通过地址栏的方式传递参数
//response.sendRedirect("UserAdmin2.jsp?username="+username+"&quanxian="+quanxian+"");
request.getRequestDispatcher("session/UserAdmin.jsp").forward(request,
response);
}
}
}
/*
public class UserAdminServlet2 extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String quanxian = request.getParameter("quanxian");
if ("commom".equals(quanxian)) {
if ("commom".equals(username) && "commom".equals(password)) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setQuanxian(quanxian);
HttpSession session = request.getSession();
session.setAttribute("commom", user);
request.getRequestDispatcher("UserAdminSuccess.jsp").forward(
request, response);
} else {
response.sendRedirect("UserAdmin2.jsp?username="+username+"&quanxian="+quanxian+"");
// request.setAttribute("username", username);
// request.setAttribute("quanxian", quanxian);
//
// request.getRequestDispatcher("UserAdmin.jsp").forward(request,
// response);
}
} else if ("admin".equals(quanxian)) {
if ("admin".equals(username) && "admin".equals(password)) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setQuanxian(quanxian);
HttpSession session = request.getSession();
session.setAttribute("commom", user);
request.getRequestDispatcher("UserAdminSuccess.jsp").forward(
request, response);
} else {
response.sendRedirect("UserAdmin2.jsp?username="+username+"&quanxian="+quanxian+"");
// request.setAttribute("username", username);
// request.setAttribute("quanxian", quanxian);
// request.getRequestDispatcher("UserAdmin.jsp").forward(request,
// response);
}
} else if ("".equals(username) || "".equals(password)) {
response.sendRedirect("UserAdmin2.jsp?username="+username+"&quanxian="+quanxian+"");
// request.getRequestDispatcher("UserAdmin.jsp").forward(request,
// response);
}
}
}
*
*
*/
4、UserAdminSuccess.jsp
<body>
<%
User user = (User)session.getAttribute("user");
if(null == user){
response.sendRedirect("/Day08/session/UserAdmin.jsp");
return;
}
%>
<a href = "QueryServlet">Query</a><br>
<!-- 做个判断是否显示Update -->
<%
if(((User)session.getAttribute("user")).getQuanxian().equals("admin")){
%>
<a href = "UpdataServlet">Updata</a><br>
<% } %>
</body>
5、UpdataServlet
package com.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 javax.servlet.http.HttpSession;
import com.javabean.User;
public class UpdataServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
HttpSession session = req.getSession();
// 获取user的权限是否是admin?
if(null == session.getAttribute("user") || !(((User)session.getAttribute("user")).getQuanxian()).equals("admin")){
System.out.println("失败");
}else{
req.getRequestDispatcher("session/Updata.jsp").forward(req, resp);
System.out.println("成功");
}
}
}
6、UpData.jsp
<body>
<%
User user = (User)session.getAttribute("user");
%>
<p>以前的session中的信息为:</p><br>
username = <%= user.getUsername() %><br>
password = <%= user.getPassword() %><br>
权限:<%= user.getQuanxian() %><br>
<p>请输入修改的信息:</p><br>
<form action="UpDataControl">
username : <input type = "text" name = "upDataUserName"><br>
password : <input type = "password" name = "upDataPassword"><br>
<input type = "submit" value = "更新数据">
</form>
</body>
7、UPdataControl
package com.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 javax.servlet.http.HttpSession;
import com.javabean.User;
public class UpDataControl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
User upDataUser = new User();
upDataUser.setUsername(req.getParameter("upDataUserName"));
upDataUser.setPassword(req.getParameter("upDataPassword"));
HttpSession session = req.getSession();
session.setAttribute("user", upDataUser);
req.getRequestDispatcher("session/UpDataMessage.jsp").forward(req, resp);
}
}
8、UpdataMessage.jsp
<body>
<%
User user = (User)session.getAttribute("user");
%>
<p>现在的session中的信息为:</p><br>
username = <%= user.getUsername() %><br>
password = <%= user.getPassword() %><br>
</body>