登录验证servlet实现

做web开发,登录验证是免不了的,今天学习了servlet的登录验证,当然是很简单的,没有使用session,request等作用域对象,所以还是可以直接通过地址访问网页的。

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();

		}

	}

}


servletDemo工程代码下载地址:

 


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>


:

 


 

你可能感兴趣的:(servlet)