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();
}
}
}
這個問題是怎麼造成的,我該如何解決呢?