The jsp session completes the login function and jspsession logs in

Source: Internet
Author: User

The jsp session completes the login function and jspsession logs in

Login. jsp:

1 <% @ page language = "java" import = "java. util. * "contentType =" text/html; charset = UTF-8 "%> 2 <% 3 String path = request. getContextPath (); 4 String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; 5%> 6 7 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 8 

Welcom. jsp

1 <% @ page language = "java" import = "java. util. * "contentType =" text/html; charset = UTF-8 "%> 2 <% 3 String path = request. getContextPath (); 4 String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; 5%> 6 7 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 8 

Web. xml

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5"  3     xmlns="http://java.sun.com/xml/ns/javaee"  4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7   <display-name></display-name>     8   <welcome-file-list> 9     <welcome-file>index.jsp</welcome-file>10   </welcome-file-list>11   <servlet>12       <servlet-name>CheckUser</servlet-name>13       <servlet-class>servlet.CheckUser</servlet-class>14   </servlet>15   <servlet-mapping>16       <servlet-name>CheckUser</servlet-name>17       <url-pattern>/check</url-pattern>18   </servlet-mapping>19   20 </web-app>

CheckUser. java

 1 package servlet; 2  3 import java.io.IOException; 4  5 import javax.servlet.RequestDispatcher; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 import javax.servlet.http.HttpSession;11 12 public class CheckUser extends HttpServlet {13     @Override14     protected void doPost(HttpServletRequest request,15             HttpServletResponse response) throws ServletException, IOException {16             request.setCharacterEncoding("UTF-8");17             String userId=request.getParameter("userId");18             String passwd=request.getParameter("passwd");19             20             if(userId!=null&&passwd!=null&&userId.equals("gys")&&passwd.equals("gys")){21                 HttpSession session=request.getSession();22                 session.setAttribute("user", userId);23                 RequestDispatcher dispatcher=request.getRequestDispatcher("/welcome.jsp");24                 dispatcher.forward(request, response);25             }26             else{27                 RequestDispatcher dispatcher=request.getRequestDispatcher("/login.jsp");28                 dispatcher.forward(request, response);29             }30     }31 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.