用户可以选择管理员和游客两种登录方式,管理员必须输入正确密码,游客不能输入任何密码
项目结构
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="userInfo" class="beans.UserBean" scope="session"/>
<%request.setCharacterEncoding("utf-8");%>
<jsp:setProperty name="userInfo" property="*"/>
<%
String valueoption = (String) request.getAttribute("uid");
String option1 = "管理员";
String option2 = "游客";
String valueoption1 = "admin";
String valueoption2 = "guest";
if ("guest".equals(valueoption)) {
option1 = "游客";
option2 = "管理员";
valueoption1="guest";
valueoption2="admin";
}
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>系统登录</title>
</head>
<body>
<h1>请输入你的登录信息</h1>
<div style="color:red"><%=request.getAttribute("errMsg") == null ? "" : request.getAttribute("errMsg")%></div>
<form method="POST" action="Checklogin">
<p>账号:
<select name="uid" style="width: 100px">
<option value= <%=valueoption1%>> <%=option1%></option>
<option value= <%=valueoption2%>><%=option2%></option>
</select>
</p>
<p>口令:<input style="width: 100px" type="password" name="pwd" value=<%=request.getAttribute("pwd") == null ?"": request.getAttribute("pwd")%> /></p>
<div><input type="submit" value="确定" /></div>
</form>
</body>
</html>
Checklogin.java
package beans;
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;
public class Checklogin extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("utf-8");
String uid = request.getParameter("uid");
String pwd = request.getParameter("pwd");
String errMsg = "";
if ("admin".equals(uid)) {
if ("1234567".equals(pwd)) {
response.sendRedirect("admin.jsp");
return;
}
errMsg = "管理员口令不正确!";
request.setAttribute("uid", uid);
request.setAttribute("pwd", pwd);
request.setAttribute("errMsg",errMsg);
request.getRequestDispatcher("index.jsp").forward(request,response);
return;
} else if ("guest".equals(uid)) {
if (pwd == null || pwd.isEmpty()) {
//口令不输入,得到的口令是长度为0的字符串
//登录成功
response.sendRedirect("admin.jsp");
return;
}
errMsg = "游客不能也无需输入口令!";
request.setAttribute("errMsg",errMsg);
request.setAttribute("uid", uid);
request.setAttribute("pwd", pwd);
request.getRequestDispatcher("index.jsp").forward(request, response);
return;
}
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Checklogin</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Checklogin at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
UserBean.java
package beans;
public class UserBean {
private String pwd;
private String uid;
public UserBean() {
}
public UserBean(String pwd, String uid) {
this.pwd = pwd;
this.uid = uid;
}
/**
* @return the pwd
*/
public String getPwd() {
return pwd;
}
/**
* @param pwd the pwd to set
*/
public void setPwd(String pwd) {
this.pwd = pwd;
}
/**
* @return the uid
*/
public String getUid() {
return uid;
}
/**
* @param uid the uid to set
*/
public void setUid(String uid) {
this.uid = uid;
}
}
admin.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="userInfo" class="beans.UserBean" scope="session"/>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>系统管理</title>
</head>
<body>
<h1>登录成功!<br>你的登录账号是:
<jsp:getProperty name="userInfo" property="uid"/>
</h1>
<br>
<p>
程序制作人信息:<br>
姓名:申栋<br>
</p>
</body>
</html>
运行结果