|
在jsp中用bean和servlet联合实现用户注册、登录(9) 2、本例主要研究注册的过程,其中的Email检测等方法并不完善,若要应用请自行设计方法
四、编写用户登陆的Servlet:login.java
//login.java
//import required classes import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*;
//class login public class login extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException { String username = req.getParameter("username"); String password = req.getParameter("password"); if(this.checklogin(username,password)) { Cookie mylogin = new Cookie("username",username); mylogin.setVersion(1); mylogin.setPath("/"); mylogin.setComment("Your login username"); res.addCookie(mylogin); } //Cookie[] myCookies = req.getCookies(); //String nameValue = this.getCookieValue(myCookies,"username","not found"); //PrintWriter out = res.getWriter(); //out.println("username" + ":" + nameValue); //out.println("Test Cookie SUCcess!"); res.sendRedirect("/index.jsp"); } public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException { doGet(req,res); } public static String getCookieValue(Cookie[] cookies,String cookieName,String defaultValue) { for(int i=0;i<cookies.length;i++) { Cookie cookie = cookies[i]; if (cookieName.equals(cookie.getName())) return(cookie.getValue()); } return(defaultValue); }
public boolean checklogin(String username,String password) { try{ DBConn loginConn = new DBConn();
|