程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> java中servlet實現登錄驗證的方法

java中servlet實現登錄驗證的方法

編輯:JAVA編程入門知識

login.java:
代碼如下:

package com.ncu;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class login extends HttpServlet{
 public void doGet(HttpServletRequest req,HttpServletResponse res){
  try{
   //res.setCharacterEncoding("utf-8");
   res.setContentType("text/html;charset=gbk");
   PrintWriter pw=res.getWriter();
   //返回登錄界面
   pw.print("<html>");
   pw.print("<head>");
   pw.print("<title>登錄界面</title></head>");
   pw.print("<body>");
   pw.print("<h1>用戶登錄界面</h1>");
   pw.print("<form action='loginpro' method=post>");
   pw.print("用戶名:<input type=text name=userName><br>");
   pw.print("密碼:<input type=password name=Pwd><br>");
   pw.print("<input type=submit value=submit>");
   pw.print("</body></html>");

   
  }catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}

LoginPro.java:
代碼如下:

package com.ncu;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginPro extends HttpServlet{
 public void doPost(HttpServletRequest req,HttpServletResponse res){
  try{
   String user=req.getParameter("userName");
   String pwd=req.getParameter("Pwd");
   if(user==null){
    user="";
   }
   if(pwd==null)
   {
    pwd="";
   }
   if(user.equals("nicolas")&&pwd.equals("nicolas")){
    //用戶和密碼合法
    //res.sendRedirect("wel");
//    res.setCharacterEncoding("gbk");
//    PrintWriter pw=res.getWriter();
//    pw.print("恭喜你,登錄成功!!");
    res.sendRedirect("welcome");

   }
   else{
    //不合法進行跳轉

    res.sendRedirect("login");//跳轉到登錄界面
   }

   
  }catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}

Welcome.java:

代碼如下:

package com.ncu;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Welcome extends HttpServlet{
 public void doGet(HttpServletRequest req,HttpServletResponse res){
  try{
   res.setCharacterEncoding("gbk");
   PrintWriter pw=res.getWriter();
   pw.print("welcome,您好");

  }catch(Exception e)
  {
   e.printStackTrace();
  }
 }
}

web.xml
代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  <!-- 給你的servlet取名,任意的 -->
  <servlet-name>hello</servlet-name>
  <!-- 指明servlet的路徑(包名+類名 -->
  <servlet-class>com.ncu.Hello</servlet-class>

  </servlet>
   <servlet-mapping>
   <!-- 這個servlet名必須和映射的一致 -->
     <servlet-name>hello</servlet-name>

  <!-- 這是在浏覽器中輸入的訪問該servlet的url -->
  <url-pattern>/nicolas</url-pattern>
  </servlet-mapping>
  <servlet>
  <servlet-name>helloservlet</servlet-name>
  <servlet-class>com.ncu.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>helloservlet</servlet-name>
  <url-pattern>/helloservlet</url-pattern>
  </servlet-mapping>
  <servlet>
  <servlet-name>helloHttpServlet</servlet-name>
  <servlet-class>com.ncu.helloHttpServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>helloHttpServlet</servlet-name>
  <url-pattern>/helloHttpServlet</url-pattern>
  </servlet-mapping>
  <servlet>
  <servlet-name>login</servlet-name>
  <servlet-class>com.ncu.login</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>login</servlet-name>
  <url-pattern>/login</url-pattern>
  </servlet-mapping>
  <servlet>
  <servlet-name>loginpro</servlet-name>
  <servlet-class>com.ncu.LoginPro</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>loginpro</servlet-name>
  <url-pattern>/loginpro</url-pattern>
  </servlet-mapping>
  <servlet>
  <servlet-name>welcome</servlet-name>
  <servlet-class>com.ncu.Welcome</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>welcome</servlet-name>
  <url-pattern>/welcome</url-pattern>
  </servlet-mapping>
</web-app>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved