jsp工作总结(5)
2016-10-4编辑:sunny
用户登录servlet********************************************************
package servlet; import java.sql.*; import java.util.*;import javax.servlet.*;
import javax.servlet.http.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;
import java.io.*;import bean.DBBean;import bean.userinfoBean;
public class userlogin extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException{}
public void service(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException{
ServletContext application=getServletContext();
//String password=(String)application.getAttribute("password");
DBBean DBcon=new DBBean();userinfoBean userinfo=new userinfoBean();
DBcon.Initialization("sun.jdbc.odbc.JdbcOdbcDriver","jdbc:odbc:ACCESSDB","llg","790325");String userid=request.getParameter("userid"); //获取从表单来的用户名
String password=request.getParameter("password"); //获取从表单来的用户密码
HttpSession session=request.getSession(true);
userid=new String(userid.getBytes("ISO8859_1"));//将密码的字符转换,解决汉字问题
PrintWriter out;response.setContentType("text/html;charset=gb2312");
out=response.getWriter();String id,passw;String prio="";String sql="select * from userDB";
String upd="Insert Into liuyan(ID,UserId,UserString) VALUES(0,'"+userid+"','加入');";
int flag=0;ResultSet rs=null;rs=DBcon.executeQuery(sql); try{while(rs.next())
{id=rs.getString("id");passw=rs.getString("password"); prio=rs.getString("priority");
if(userid.equals(id)&&password.equals(passw)) //如果用户名和密码都正确
{flag=1;break;}}if(flag==1){ userinfo.setuserid(userid);
userinfo.setpriority(prio);out.println(userid+"登录成功!");DBcon.executeUpdate(upd);
session.setAttribute("userinfo",userinfo); response.sendRedirect("/frma.jsp");
}else{out.println("用户登录失败"+userid+password); //如果用户名和密码都不正确
}}catch(SQLException ex){out.println(ex.getMessage());
}}public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException{doGet(request,response);}}