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

java中servlet完成登錄驗證的辦法

編輯:關於JAVA

java中servlet完成登錄驗證的辦法。本站提示廣大學習愛好者:(java中servlet完成登錄驗證的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java中servlet完成登錄驗證的辦法正文


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