欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java中servlet实现登录验证的方法

程序员文章站 2023-12-17 08:34:04
login.java:复制代码 代码如下:package com.ncu;import java.io.printwriter; import javax.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>

上一篇:

下一篇: