下面请跟着图老师小编一起来了解下java中servlet实现登录验证的方法,精心挑选的内容希望大家喜欢,不要忘记点个赞哦!
【 tulaoshi.com - 编程语言 】
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=userNamebr");
pw.print("密码:input type=password name=Pwdbr");
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-fileindex.jsp/welcome-file
/welcome-file-list
servlet
!-- 给你的servlet取名,任意的 --
servlet-namehello/servlet-name
!-- 指明servlet的路径(包名+类名 --
servlet-classcom.ncu.Hello/servlet-class
/servlet
servlet-mapping
!-- 这个servlet名必须和映射的一致 --
servlet-namehello/servlet-name
!-- 这是在浏览器中输入的访问该servlet的url --
url-pattern/nicolas/url-pattern
/servlet-mapping
servlet
servlet-namehelloservlet/servlet-name
servlet-classcom.ncu.HelloServlet/servlet-class
/servlet
servlet-mapping
servlet-namehelloservlet/servlet-name
url-pattern/helloservlet/url-pattern
/servlet-mapping
servlet
servlet-namehelloHttpServlet/servlet-name
servlet-classcom.ncu.helloHttpServlet/servlet-class
/servlet
servlet-mapping
servlet-namehelloHttpServlet/servlet-name
url-pattern/helloHttpServlet/url-pattern
/servlet-mapping
servlet
servlet-namelogin/servlet-name
servlet-classcom.ncu.login/servlet-class
/servlet
servlet-mapping
servlet-namelogin/servlet-name
url-pattern/login/url-pattern
/servlet-mapping
servlet
servlet-nameloginpro/servlet-name
servlet-classcom.ncu.LoginPro/servlet-class
/servlet
servlet-mapping
servlet-nameloginpro/servlet-name
url-pattern/loginpro/url-pattern
/servlet-mapping
servlet
servlet-namewelcome/servlet-name
servlet-classcom.ncu.Welcome/servlet-class
/servlet
servlet-mapping
servlet-namewelcome/servlet-name
url-pattern/welcome/url-pattern
/servlet-mapping
/web-app
来源:http://www.tulaoshi.com/n/20160219/1592681.html
看过《java中servlet实现登录验证的方法》的人还看了以下文章 更多>>