这学期的期未考试完了,在JSP中最后一个题是写一个简单的登录验证。看到题后觉得很简单。用Servlet可以简单实现。题目要求有三个文件。login.jsp(登录界面)Check.java(验证机制)index.jsp(成功界面)
本想用Servlet来实现。但当动手的时候,大脑一自空白。怎么写。怎么实现,要继承的类都没有记下。基本格式都记不起来。。郁闷。最后为了应付,用了javaBean勉强实现。
下来后重新认为了一下。现作了一个较完整的列出如下。安慰自己。
本想用Servlet来实现。但当动手的时候,大脑一自空白。怎么写。怎么实现,要继承的类都没有记下。基本格式都记不起来。。郁闷。最后为了应付,用了javaBean勉强实现。
下来后重新认为了一下。现作了一个较完整的列出如下。安慰自己。
/**
login.jps
*/
< html >
< head >
< title > 用户登录 </ title >
</ head >
< body >
< p > 输入用户名密码 </ p >
< form name ="form1" method ="get" action ="check" >
< p > 用户: < input type ="text" name ="user" ></ p >
< p > 密码: < input type ="password" name ="pwd" ></ p >
< br >
< input type ="submit" name ="submit" value ="提交" >
< input type ="reset" name ="reset" value ="重置" >
</ form >
</ body >
</ html >
login.jps
*/
< html >
< head >
< title > 用户登录 </ title >
</ head >
< body >
< p > 输入用户名密码 </ p >
< form name ="form1" method ="get" action ="check" >
< p > 用户: < input type ="text" name ="user" ></ p >
< p > 密码: < input type ="password" name ="pwd" ></ p >
< br >
< input type ="submit" name ="submit" value ="提交" >
< input type ="reset" name ="reset" value ="重置" >
</ form >
</ body >
</ html >
/** */
/**
Check.java
*/
import java.io. * ;
import javax.servlet. * ;
import javax.servlet.http. * ;
public class Check extends HttpServlet
... {
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
...{
String user=req.getParameter("user");
String pwd =req.getParameter("pwd");
HttpSession session = req.getSession();
if(user.equals("admin") && pwd.equals("admin"))
...{
session.setAttribute("login","ok");
res.sendRedirect("index.jsp");
}
else
...{
res.sendRedirect("login.jsp");
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
...{
doGet(req,res);
}
}
Check.java
*/
import java.io. * ;
import javax.servlet. * ;
import javax.servlet.http. * ;
public class Check extends HttpServlet
... {
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
...{
String user=req.getParameter("user");
String pwd =req.getParameter("pwd");
HttpSession session = req.getSession();
if(user.equals("admin") && pwd.equals("admin"))
...{
session.setAttribute("login","ok");
res.sendRedirect("index.jsp");
}
else
...{
res.sendRedirect("login.jsp");
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws IOException,ServletException
...{
doGet(req,res);
}
}
/**
index.jsp
*/
< html >
< head >
< title > 欢迎界面 </ title >
</ head >
< body >
<% ...
Object obj=session.getAttribute("login");
if(obj==null)
response.sendRedirect("login.jsp");
else if(obj.toString()=="ok")
out.println("欢迎admin");
else
response.sendRedirect("login.jsp");
%>
</ body >
</ html >
index.jsp
*/
< html >
< head >
< title > 欢迎界面 </ title >
</ head >
< body >
<% ...
Object obj=session.getAttribute("login");
if(obj==null)
response.sendRedirect("login.jsp");
else if(obj.toString()=="ok")
out.println("欢迎admin");
else
response.sendRedirect("login.jsp");
%>
</ body >
</ html >
/**
web.xml
*/
<? xml version="1.0" encoding="ISO-8859-1" ?>
< web-app 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_2_5.xsd"
version ="2.5" >
< servlet >
< servlet-name > check </ servlet-name >
< servlet-class > Check </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > check </ servlet-name >
< url-pattern > /check </ url-pattern >
</ servlet-mapping >
</ web-app >
web.xml
*/
<? xml version="1.0" encoding="ISO-8859-1" ?>
< web-app 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_2_5.xsd"
version ="2.5" >
< servlet >
< servlet-name > check </ servlet-name >
< servlet-class > Check </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > check </ servlet-name >
< url-pattern > /check </ url-pattern >
</ servlet-mapping >
</ web-app >