Objective: Familiar session of the two functions, the difference between request and session scope of use

  1. Create a login page login.html
  2. Provide a user name input box, and the login button
  3. Create a login page processing login.jsp
  4. If the user name ADMIN , the user name is stored in the session in and jump into the index.jsp in
  5. Otherwise, jump into the login.html page
  6. Create a page index.jsp
  7. If the session is not the user information, to jump into the login.html in
  8. Otherwise, display a welcome message
  •  

     

     

     The first to write servlet page is the most important pages

package net.wanho.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;

/**
 * Servlet implementation class FourServlet
 */
public class FourServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取得页面上的值
        request.setCharacterEncoding("utf-8");
    String username =     request.getParameter("username");
    String password =     request.getParameter("pwd");
        //和init-param比对
    String name =     this.getInitParameter("name");
    String pwd =     this.getInitParameter("pwd");
    
    IF (username.contentEquals (name) && password.equals (pwd)) {
         // If the user name will be stored into the session matching 
        the HttpSession session = Request.getSession ();
        session.setAttribute("uname", username);
        //跳转到主页面
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }else {
        response.sendRedirect("login.jsp");
    }
    
        
        
        
        
        // the session objects can do
         // the session used to keep the value range than the request should be broad, open the browser, connect the site to the browser is closed
        
        // can save the user's login information, shopping cart 
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

jsp login page

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
欢迎:<%=session.getAttribute("uname") %>
<br>
<form action="fourServlet" method="post">
    用户名<input type="text" name="username"><br>
    密码<input type="password" name="pwd"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

Successful login page

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body> 
    Welcome (session value): <% = session.getAttribute ( "uname")%>
    <br> 
    welcome you (request value): <% = request.getParameter ( "username")%>
</body>
</html>

web.xml configuration information

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>HomeWork4</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>FourServlet</display-name>
    <servlet-name>FourServlet</servlet-name>
    <servlet-class>net.wanho.servlet.FourServlet</servlet-class>
     <init-param>
         <param-name>name</param-name>
         <param-value>admin</param-value>
     </init-param>
 <init-param>
         <param-name>pwd</param-name>
         <param-value>123456</param-value>
     </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>FourServlet</servlet-name>
    <url-pattern>/fourServlet</url-pattern>
  </servlet-mapping>
</web-app>

Also to be there to appreciate the main request and session scope of use, request only once, when the request data again disappears, and session data can be saved. Unless the browser is closed, session data is lost.

Guess you like

Origin www.cnblogs.com/sunstudy/p/12317066.html