Reffer to the book<java web整合开发王者归来>.
It‘s jsp page.
Offer the values of username and password to servlet.
Then save the session.
The code is simple.
easy to read.
Except the myLogin.jsp,also need a javabean Person.java.
<%@ page language="java" pageEncoding="UTF-8"%> <jsp:directive.page import="com.helloweenvsfei.sessionWeb.bean.Person" /> <jsp:directive.page import="java.text.SimpleDateFormat" /> <jsp:directive.page import="java.text.DateFormat" /> <jsp:directive.page import="java.util.Date" /> <%! DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); %> <% response.setCharacterEncoding("UTF-8"); Person[] persons = { new Person("linghuchong", "linghuchong", 34, dateFormat.parse("1982-01-01")), new Person("Hello Kitty", "hellokitty", 23, dateFormat.parse("1984-02-25")), new Person("renyingying", "renyingying", 23, dateFormat.parse("1994-09-12")), }; String message = ""; if(request.getMethod().equals("POST")) { for(Person person : persons) { if(person.getName().equalsIgnoreCase(request.getParameter("username")) && person.getPassword().equals(request.getParameter("password"))) { session.setAttribute("person", person); session.setAttribute("loginTime", new Date()); response.sendRedirect(request.getContextPath() + "/welcome.jsp"); return; } } // login failed message = "wrong username or pwd,login failed"; } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Please login first</title> </head> <body> <form action="myLogin.jsp" method="post"> <table> <% if( ! message.equals("")){ %> <tr> <td></td> <td>login failed!</td> </tr> <% } %> <tr> <td>username:</td> <td><input type="text" name="username" style="width:200px; "> </td> </tr> <td>password:</td> <td><input type="password" name="password" style="width:200px; "> </td> <tr> <td></td> <td><input type="submit" value=" Login " class="button"> </td> </tr> </table> </form> Hello Kitty, hellokitty </body> </html>
时间: 2024-10-10 16:11:44