我这段代码是实现登录功能,项目按MVC架构来做的,
//servlet层
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();

String operation=request.getParameter("operation");

if(operation.equals("user_login"))
{
UserLogin(request,response);
}
}       //用户登录
public void UserLogin(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
String login_name=request.getParameter("username");
String login_pwd=request.getParameter("password");

olinebookstoresdto userlogin=new olinebookstoresdto();
useroperation login=new useroperation();
userlogin=login.showuser(login_name,login_pwd);
if(userlogin!=null){
HttpSession session=request.getSession();
session.setAttribute("username", userlogin); out.print("<script language=javascript>location.href='../index.jsp';</script>");
}
else
{ out.print("<script language=javascript>location.href='../error/error.jsp';</script>");
} }
//servince层
private olinebookstoresinterface user=new userimpl();//会员登录
public olinebookstoresdto showuser(String id,String pwd)
{
Connection conn=null;
olinebookstoresdto userlogin=null;
try
{
conn=database.getConn();
user.setconnection(conn);
userlogin=user.showuser(id,pwd);
}
catch(Exception e)
{
e.getMessage();
}
finally
{
database.releaseConnection(conn);
}
return userlogin;
}//DAO层import java.sql.Connection;import com.OlineBookStores.DTO.olinebookstoresdto;
public interface olinebookstoresinterface {
public void userenall(olinebookstoresdto userenall);
public olinebookstoresdto showuser(String id,String pwd);
public Connection getconnection();
public void setconnection(Connection connection);
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import com.OlineBookStores.DAO.olinebookstoresinterface;
import com.OlineBookStores.DTO.olinebookstoresdto;
import com.OlineBookStores.DB.database;
/**
 * @author Glary days
 *
 */
public class userimpl implements olinebookstoresinterface { /**
 * 
 */
Connection conn=null;
public userimpl() {
try
{
conn=database.getConn();
}
catch(SQLException sql)
{
sql.printStackTrace();
}
} /* (non-Javadoc)
 * @see com.OlineBookStores.DAO.olinebookstoresinterface#getconnection()
 */
public Connection getconnection() {

return conn;
} /* (non-Javadoc)
 * @see com.OlineBookStores.DAO.olinebookstoresinterface#setconnection(java.sql.Connection)
 */
public void setconnection(Connection conn) {

        this.conn=conn;
} /* (non-Javadoc)
 * @see com.OlineBookStores.DAO.olinebookstoresinterface#userenall(com.OlineBookStores.DTO.olinebookstoresdto)
 */


public static void closeStatement(Statement st) {
if (st != null) {
try {
st.close();
st = null;
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
}
    
//用户登录
public olinebookstoresdto showuser(String id,String pwd) {

PreparedStatement pst=null;
ResultSet rs=null;
olinebookstoresdto userlogin=null;
try
{
pst=conn.prepareStatement("select * from Memeber where mb_mail=? and mb_password=?");

int i=1;
pst.setString(i++,id);
pst.setString(i++, pwd);
rs=pst.executeQuery();

while(rs.next())
{
userlogin=new olinebookstoresdto();

userlogin.setUserID(rs.getString("userID"));
userlogin.setPwd(rs.getString("pwd"));
}
}
catch(Exception e)
{
e.getMessage();
}
finally
{
closeStatement(pst);
closeResultSet(rs);
}
return userlogin;
}}//DTO层public class olinebookstoresdto {

private String email;
private String userID;
private String pwd;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}//JSP登录界面
  <form name="operation" method="post" action="<%=request.getContextPath()%>/servlet/useraction">
                                 <input type="hidden" name="operation"/>
                               <div id="ccmrlcontent"> 
                                    <div id="ccmrlcleft">
                                        <div id="ccmrlctypeone" class="ccmrlctypepadding">账号
                                        </div>
                                        <div id="ccmrlctypetwo" class="ccmrlctypepadding"><input type="text" name="username" width="80" height="10"/>
                                        </div>
                                        <div id="ccmrlctypethree">密码
                                        </div>
                                        <div id="ccmrlctypeforth"><input type="password" name="password" width="80" height="10"/>
                                        </div>
                                    </div>
                                    <div id="ccmrlcright">
                                        <div id="ccmrlcrightbuttom">
                                            <a href="<%=request.getContextPath()%>/servlet/useraction?operation=user_login"><img src="../Images/managerlogin/login01-1_r4_c7.gif"/></a>
                                        </div>
                                    </div>                     
                               </div>
                              </form> 主界面
 <div id="headerupleft">
                <%if(session.getAttribute("username")==null){ %>
                   <p class="text">欢迎光临乐购网: [ <a href="admin/userlogin.jsp" target="_self" name="login">登录</a> | <a href="Other pages/enall/enall.jsp" target="_self" name="enroll">注册</a> ]</p>
                   <%}
                     else{%>
                    <p class="text">您好:<%=session.getAttribute("username")%>[<a href="#" target="_self" name="out">退出</a>]</p>
                    <%}%>
                </div>
我这段代码是为了实现类似于当当网首页登录的效果,但是每次我在登陆界面输入账号密码,都跳转到错误界面error.jsp,并返回登陆界面,返回登陆界面是在error.jsp里用JAVASCRIPT实现的,
请各位大哥帮帮忙,谢谢

解决方案 »

  1.   


    olinebookstoresdto userlogin=new olinebookstoresdto();
    useroperation login=new useroperation();
    userlogin=login.showuser(login_name,login_pwd);
    if(userlogin!=null){
    HttpSession session=request.getSession();
    session.setAttribute("username", userlogin);out.print("<script language=javascript>location.href='../index.jsp';</script>");
    }
    else
    {out.print("<script language=javascript>location.href='../error/error.jsp';</script>");
    }
    从上面可以看出是失败才跳转到error.jsp的 代码上没什么问题 检查下数据库里用户名和密码是否相符