一般的HTML表单,并按下面的要求定义一个Servlet,响应上面的登录表单请求。
该Servlet的类名自定义,并采用注解方式配置其访问的URL;
在注解中定义两个Servlet初始化参数,两个参数的参数名与参数值分别是:userName="admin" 和 pwd="123456"
在Servlet中重写init方法,在init方法中通过ServletConfig对象获取注解中定义的两个初始化参数;
在Servlet中处理响应表单提交的请求,获取用户在表单提交的用户名和密码,并和初始化的用户名和密码进行匹配,如果两者一样,就输出“登录成功”,并在5秒钟后跳转到home.jsp页面,反之则立即跳转到login.jsp页面。
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Date;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
@WebServlet(
urlPatterns = "/login.do",
initParams = {
@WebInitParam(name="userName",value = "admin"),
@WebInitParam(name="pwd",value="123456")
}
)
public class servlet1 extends HttpServlet{
private String str1;
private String str2;
public void init(ServletConfig config) {
str1=config.getInitParameter("userName");
str2=config.getInitParameter("pwd");
}
public void service(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
response.setContentType("text/html;charset=utf-8");
PrintWriter printWriter=response.getWriter();
String name=request.getParameter("userName");
String pwd=request.getParameter("password");
if(name.equals(str1)&&pwd.equals(str2))
{
printWriter.print("登录成功!");
printWriter.print("<meta http-equiv=\"refesh\" content=\"5;url=home.jsp\">");
}
else {
response.sendRedirect("login.jsp");
}
}
}
cookie记录登录时间 每次刷新显示上次登录时间:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;
import javax.websocket.Session;
@WebServlet("/cookies")
public class servlet1 extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html;charset=UTF-8");
SimpleDateFormat dateFormat=new SimpleDateFormat("hh:mm:ss");
String string=dateFormat.format(new Date());
PrintWriter ptPrintWriter=response.getWriter();
Cookie [] cookies=new Cookie[100];
Cookie[] cookies2=request.getCookies();
if(cookies2==null)
{
ptPrintWriter.print("您是第一次访问本页面");
cookies[0]=new Cookie("name"+0, string);
response.addCookie(cookies[0]);
}
else {
int i=0;
cookies[i]=new Cookie("name"+i, string);
response.addCookie(cookies[i]);
i++;
ptPrintWriter.print("您上一次访问本页面的时间是:"+cookies2[i-1].getValue());
}
}
}
|