2011-05-30 71 views
0

我有一個JSP文件Signup.jsp,它提交給UserDetails servlet,它將用戶詳細信息插入數據庫「tblusers」,並在成功插入數據後轉發到ElecBill.jsp。它轉發到Signup.jsp而不是轉發到ElecBill.jsp。換句話說,插入數據失敗了。Servlet未轉發到另一個JSP

下面是servlet代碼:

package com.elecbill; 

import java.io.*; 
import java.sql.*; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 


public class UserDetails extends HttpServlet 
{ 
public void doPost(HttpServletRequest request,HttpServletResponse response) 
throws javax.servlet.ServletException, IOException 
{ 

String userid=request.getParameter("uid"); 
String firstname=request.getParameter("fname"); 
String lastname=request.getParameter("lname"); 
String phonenum=request.getParameter("phone"); 
String mobilenum=request.getParameter("mobile"); 
String email=request.getParameter("email"); 
String login=request.getParameter("loginid"); 
String pass=request.getParameter("pass"); 
String pass1=request.getParameter("pass1"); 
try 
{ 

Class.forName("com.mysql.jdbc.Driver"); 
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/elecbill","root","root");  
String query="insert into tblusers('"+userid+"','"+firstname+"','"+lastname+"','"+phonenum+"','"+mobilenum+"','"+email+"','"+login+"','"+pass+"','"+pass1+"')"; 

Statement s=conn.createStatement(); 
ResultSet rs=s.executeQuery("select * from tblusers"); 
boolean str=false; 
while(rs.next()) 
{ 
    if((rs.getString(6).equals(login))) 
    { 
     str=true; 
    } 
} 
    if(!str) 
{ 
    s.executeUpdate(query); 
    HttpSession ses=request.getSession(true); 
    ses.setAttribute("login",login); 
    RequestDispatcher rq=request.getRequestDispatcher("ElecBill.jsp"); 
    request.setAttribute("msg","Registered Successfully"); 
    rq.forward(request,response); 
    System.out.println("Successfully"); 

} 
    else 
{ 
RequestDispatcher rq=request.getRequestDispatcher("signup.jsp"); 
request.setAttribute("msg","Im Already in use. Choose another Id"); 
rq.forward(request,response); 


} 

    } 
    catch (Exception e) 
    { 
e.printStackTrace(); 
    } 

} 

} 

這個問題是怎麼造成的,我該如何解決呢?

回答

2

插入數據沒有失敗。更重要的是,根據代碼流程,它根本沒有被執行。根據您的代碼流,這意味着用戶提供的loginid參數與其中一個錶行的第6列的值匹配。

嘗試提供不同的loginid值。


無關到混凝土的問題,這一段代碼是SQL injectionattacks敏感。使用PreparedStatement。另外,當您將整個數據庫表複製到Java內存中,並在Java中而不是在SQL中進行比較時,找到現有的登錄標識非常低效。瞭解如何使用WHERE clause並僅使用boolean exist = resultSet.next()

相關問題