目的:熟悉session的两个函数,区别request和session的使用范围

  1. 创建登录页面login.html
  2. 提供用户名输入框、以及登录按钮
  3. 创建登录处理页面login.jsp
  4. 如果用户名为admin,把用户名存储在session中,并跳转进入index.jsp
  5. 否则跳转进入login.html页面
  6. 创建页面index.jsp
  7. 如果session中没有该用户信息,跳转进入login.html
  8. 否则显示欢迎信息
  •  

     先写servlet页面,也是最重要的页面

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)) {
        //如果匹配上就将用户名存到session
        HttpSession session = request.getSession();
        session.setAttribute("uname", username);
        //跳转到主页面
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }else {
        response.sendRedirect("login.jsp");
    }
    
        
        
        
        
        //session对象能干什么
        //session用来存值,范围要比request要广,打开浏览器,连上网站开始,到浏览器关闭
        
        //可以存用户的登录信息,购物车
    }

    /**
     * @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登录页面

<%@ 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>

登录成功的页面

<%@ 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取值):<%=session.getAttribute("uname") %>
    <br>
    欢迎你(request取值):<%=request.getParameter("username") %>
</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" 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>

这里面主要还有要体会到requestsession的使用范围,request只能使用一次,当再次请求时数据就会消失,而session数据可以被保存。除非浏览器关闭,session数据消失。

原文地址:https://www.cnblogs.com/sunstudy/p/12317066.html