当前位置: 首页 > 工具软件 > mini-session > 使用案例 >

Servlet——Session会话mini项目

辛健
2023-12-01
<span style="font-size:18px;"><span style="font-size:18px;">package myservlet;
/*
 * Session 会话技术目的是服务器只能服务一个浏览器,防止非法登陆,运行在服务器端;
 */

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet1 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
	    String erro= (String)request.getAttribute("error");
	            
		out.println("<form action='/Myservlet/Servlet2' method='post'>");
		out.println("<h1>欢迎登陆</h1>");
		out.println("用户名:<input type='text' name='userid'/><br/>");
		out.println("密  码:<input type='password' name='password'/><br/>");
		out.println("<input type='submit' value='登陆'>");
		out.println("验证码<img src=''/>");
		out.println("</from>");
		 
		if(erro!=null){
	    	 out.println("<h5 style='color:red'>"+erro+"</h5>");
	     }
        
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		        this.doGet(request, response);

	}

}
+++++++++++++++


package myservlet;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        HttpSession hs = request.getSession();
        
        String userid = request.getParameter("userid");
        String password = request.getParameter("password");
        if("123".equals(password)){
            out.println("<h3>恭喜你登陆成功</h3>");
            hs.setAttribute("userid",userid);
            hs.setAttribute("password", password);
            request.getRequestDispatcher("/Servlet3").forward(request,response);
        }else{
            request.setAttribute("error","密码错误");
            request.getRequestDispatcher("/Servlet1").forward(request,response);
        }
        
        

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);

    }

}

++++++++++++++
package campus1;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Servelet3 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("登陆成功");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
     
    }

}

 
</span></span>

 类似资料: