一、建立一个名叫jsp_servlet的工程
二、建立一个AcountBean类和CheckAccount类
1、AcountBean类包含登录名(username)和登录密码(password)
public class AcountBean {
String username;
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;
}
}
2、CheckAccount类继承HttpServlet
public class CheckAccount extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); AcountBean account = new AcountBean(); String username = req.getParameter("username"); String pwd = req.getParameter("pwd"); account.setPassword(pwd); account.setUsername(username); System.out.println(username); System.out.println(pwd); if ((username != null) && (username.trim().equals("jsp"))) { if ((pwd != null) && (pwd.trim().equals("123"))) { System.out.println("success"); session.setAttribute("account", account); String login_suc = "success.jsp"; resp.sendRedirect(login_suc); return; } } String login_fail = "fail.jsp"; resp.sendRedirect(login_fail); return; } }
三、建立jsp界面
1、建立登录界面login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录系统</title> </head> <body> <form action="login"> username:<input type="text" name="username"><br> password:<input type="password" name="pwd"><br> <input type="submit"> </form> </body> </html>
2、建立登录成功界面success.jsp
<%@page import="nsu.edu.student.AcountBean"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录成功</title> </head> <body> <% AcountBean account = (AcountBean) session.getAttribute("account"); %> username:<%=account.getUsername()%> <br> password:<%=account.getPassword()%> </body> </html>
3、建立登录失败界面fail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录失败</title> </head> <body> Login Failed! <br> </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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name></display-name> <!-- 登录首页路径 --> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- 点击登录后执行Java代码的路径 --> <servlet> <!-- 类名 --> <servlet-name>CheckAccount</servlet-name> <!-- 调用的类的位置 --> <servlet-class>nsu.edu.student.CheckAccount</servlet-class> </servlet> <!-- 元素在Servlet和URL样式之间定义一个映射--> <servlet-mapping> <servlet-name>CheckAccount</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> </web-app>