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>