2017-02-21 161 views
2

It is a executed jsp file.HTTP狀態404 -/DBinsert

正如我填寫執行JSP文件中的表格,然後點擊註冊按鈕,它顯示下面的輸出頁面。

After clicking the signup button, the output page

我的servlet文件代碼

import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.swing.JOptionPane; 

import com.mysql.jdbc.Statement; 

/** 
* Servlet implementation class for Servlet: DBinsert 
* 
*/ 
public class DBinsert extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { 
    static final long serialVersionUID = 1L; 

    /* (non-Java-doc) 
    * @see javax.servlet.http.HttpServlet#HttpServlet() 
    */ 
    public DBinsert() { 
     super(); 
    }  

    /* (non-Java-doc) 
    * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

String fname=request.getParameter("fname"); 
String sname=request.getParameter("sname"); 
String user=request.getParameter("uname"); 
String pwd=request.getParameter("pwd"); 
String email=request.getParameter("email"); 
String phone=request.getParameter("phone"); 
/*int ph=Integer.parseInt(phone); 

if (phone.length()!=10) 
     { 
    JOptionPane.showMessageDialog(null,"errormsg"); 
      String e="Check phone number "; 

      request.setAttribute("errormsg", e); 
      request.getRequestDispatcher("SignNIIT.jsp").forward(request, response); 
     } 
     else 
     */{ 
      try { 
       Class.forName("com.mysql.jdbc.Driver"); 
         Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", ""); 
         String code="insert into prct.db (lastname,username,password) values (?,?,?)"; 
         PreparedStatement ps=con.prepareStatement(code); 
         //ps.setString(1, fname); 
         ps.setString(1, sname); 
         ps.setString(2, user); 
         ps.setString(3, pwd); 
         //ps.setString(5, email); 
         //ps.setString(6, phone); 
         ps.executeUpdate(); 
         //JOptionPane.showMessageDialog(null, "Sucessfull"); 
        String msg="!!! Registered Sucessfully !!! "; 
         request.setAttribute("data", msg); 
         request.getRequestDispatcher("SignU.jsp").forward(request, response); 

      } catch (Exception e) { 
       // TODO: handle exception 
       e.printStackTrace(); 
      } 
     } 
    } 

我的web.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 
    <display-name>aWebTest</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
    <description></description> 
    <display-name>DBinsert</display-name> 
    <servlet-name>DBinsert</servlet-name> 
    <servlet-class>dbAccess.DBinsert</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>DBinsert</servlet-name> 
    <url-pattern>/DBinsert</url-pattern> 
    </servlet-mapping> 
</web-app> 

我不能夠解決這個錯誤。 請幫助我。

回答

0

相反web.xml您可以使用標註(因爲servlet的3.0)。試試這個代碼。

import java.io.IOException; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.swing.JOptionPane; 

import com.mysql.jdbc.Statement; 

/** 
* Servlet implementation class for Servlet: DBinsert 
* 
*/ 
@WebServlet(name = "DBinsert", urlPatterns = { "/DBinsert" }) 
public class DBinsert extends HttpServlet { 
    static final long serialVersionUID = 1L; 

    /* (non-Java-doc) 
    * @see javax.servlet.http.HttpServlet#HttpServlet() 
    */ 
    public DBinsert() { 
     super(); 
    }  

    /* (non-Java-doc) 
    * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

String fname=request.getParameter("fname"); 
String sname=request.getParameter("sname"); 
String user=request.getParameter("uname"); 
String pwd=request.getParameter("pwd"); 
String email=request.getParameter("email"); 
String phone=request.getParameter("phone"); 
/*int ph=Integer.parseInt(phone); 

if (phone.length()!=10) 
     { 
    JOptionPane.showMessageDialog(null,"errormsg"); 
      String e="Check phone number "; 

      request.setAttribute("errormsg", e); 
      request.getRequestDispatcher("SignNIIT.jsp").forward(request, response); 
     } 
     else 
     */{ 
      try { 
       Class.forName("com.mysql.jdbc.Driver"); 
         Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", ""); 
         String code="insert into prct.db (lastname,username,password) values (?,?,?)"; 
         PreparedStatement ps=con.prepareStatement(code); 
         //ps.setString(1, fname); 
         ps.setString(1, sname); 
         ps.setString(2, user); 
         ps.setString(3, pwd); 
         //ps.setString(5, email); 
         //ps.setString(6, phone); 
         ps.executeUpdate(); 
         //JOptionPane.showMessageDialog(null, "Sucessfull"); 
        String msg="!!! Registered Sucessfully !!! "; 
         request.setAttribute("data", msg); 
         request.getRequestDispatcher("SignU.jsp").forward(request, response); 

      } catch (Exception e) { 
       // TODO: handle exception 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

由於問題覆蓋doPost方法 – Shambhu

0

我想你會把數據發送到你的Servlet,但只能覆蓋doGet -Method。

嘗試重寫doPost方法

+0

感謝答覆,但我怎樣才能改變保護無效的doGet(...)爲保護無效的doPost(...)解決 – Shambhu

+0

。 –