还是有关session的登录验证小应用


一个jsp登录页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.mison.User"%>
<html>
  <head>
  
    
    <title>My JSP 'sessionLogin.jsp' starting page</title>

  </head>
  
  <body>
       <%
       	          User user=null;
                  String name="";
                  String authority="";
                  
              if(!session.isNew()){
            	 
                  user =(User)session.getAttribute("user"); 
                  name=user.getName();
                  authority=user.getAuthority();
              }
       %>
       
      <form action="LoginCheck">
         username1:<input type="text" name="username" value="<%=name%>"><br>
         password:<input type="password" name="password" value=""><br>
         authority:
         <select name="authority">
             <option value="boss" <%="boss".equals(authority)?"selected='selected'":""%>>boss</option>
             <option value="user" <%="user".equals(authority)?"selected='selected'":"" %>>user</option>
         </select><br>
         <input type="submit" value="submit">
      </form>
  </body>
</html>


web.xml页面
<servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginCheck</servlet-name>
    <servlet-class>com.mison.LoginCheck</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>LoginCheck</servlet-name>
    <url-pattern>/LoginCheck</url-pattern>
  </servlet-mapping>


一个进行验证的servlet类
package com.mison;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class LoginCheck extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
           User user=new User();
           HttpSession session =req.getSession();
           String username=req.getParameter("username");
           String password=req.getParameter("password");
           String authority=req.getParameter("authority");
           
           if("boss".equals(authority)){
        	   if("chenjing".equals(username)&&"chenjing".equals(password)){
        		     
        		     user.setName(username);
        		     user.setPassword(password);
        		     user.setAuthority(authority);
        		     
        		     
        		     session.setAttribute("user", user);
        		    
        		     RequestDispatcher dp=req.getRequestDispatcher("success.jsp");
        		     dp.forward(req, resp);
        	   }
        	   else{
        		   user.setName(username);
            	   
        		   user.setAuthority(authority);
            	   
            	   session.setAttribute("user", user);
            	  
            	   RequestDispatcher dp=req.getRequestDispatcher("sessionLogin.jsp");
            	   
            	   dp.forward(req, resp);
        	   }
        	   
           }
           else if("user".equals(authority)){
        	   if("chenfeng".equals(username)&&"chenfeng".equals(password)){
        		 
        		 user.setName(username);
      		     user.setPassword(password);
      		     user.setAuthority(authority);
      		     
      		     
      		     session.setAttribute("user", user);
        		 
      		     RequestDispatcher dp=req.getRequestDispatcher("success.jsp");
        		 dp.forward(req, resp);
        	   }
        	   else{
        		   user.setName(username);
            	   
        		   user.setAuthority(authority);
            	   
            	   session.setAttribute("user", user);
            	  
            	   RequestDispatcher dp=req.getRequestDispatcher("sessionLogin.jsp");
            	   
            	   dp.forward(req, resp);
        		   
        	   }
        	   
           }
         
	}

}



一个进行存取用户名,用户类型的javabean类
package com.mison;

public class User {
    private String name;
  	private String password;
    private String authority;
      public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getAuthority() {
		return authority;
	}
	public void setAuthority(String authority) {
		this.authority = authority;
	}

}


一个显示正确结果的jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.mison.User" %>

<html>
  <head>
    
    
    <title>My JSP 'success.jsp' starting page</title>
    


  </head>
  
  <body>  
               <%
                 	User user =(User)session.getAttribute("user"); 
                                  String username =user.getName();
                                  String authority =user.getAuthority();
                 %>
                                您好:<%=username%> <br> 欢迎您,您是<%=authority%>;可以直接登录!
  </body>
</html>



你可能感兴趣的:(java,jsp,xml,Web,servlet)