} System.out.println(newreg.getID()); }catch(Exception e){ System.err.println(e.toString()); } } };
说明: 1、该bean文件应和上文所述DBConn.class文件放于同一目录下 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(); loginConn.executeQuery("select * from tbl_user where name='" + username + "'"); if(loginConn.rs_next()) { System.out.println("Connection created!"); if(loginConn.rs_getString("pwd").trim().equals(password)) { System.out.println(loginConn.rs_getString("name"));
上一篇:新版PHP极大的增强功能和性能
下一篇:在Linux环境下安装JSP
|