我正在尝试创建一个servlet,它使用类GetTicketInfo从HTML表单中读取数据,并使用WriteToDatabase类将数据插入到MySQL数据库中。
问题是当我填写HTML表单并按下Register Ticket按钮时,它应该开始将数据插入数据库的过程中我在浏览器中收到此错误“HTTP状态405 - HTTP此网址“不支持GET方法。我的index.html的URL(包含表单)是http://localhost/IssueHandler/index.html
我使用Apache Tomcat 7.0.34服务器
控制台不会打印任何错误,而是两个警告:
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CDM' did not find a matching property.
jan 12, 2015 5:42:06 EM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:IssueHandler' did not find a matching property.
jan 12, 2015 5:42:07 EM org.apache.catalina.core.AprLifecycleListener initializeSSL
我猜它与无法找到servlet的表单有关,但我老实说不知道。我已经尝试将servlet-mapping标签等添加到web.xml文件中,但它没有帮助。
GetTicketInfo
package com.issue.handler;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
@WebServlet("/issueHandler")
public class GetTicketInfo extends HttpServlet {
String ticketID1, ticketOwner1, ticketTitle1, ticketHandler1, ticketCategory1, ticketDescription1, ticketDate1;
public void doGet(HttpServletRequest request,
HttpServletResponse response, String tID) {
tID=request.getParameter("ticketID");
ticketID1=tID;
}
public String getTicketID(){
return ticketID1;
}
public void setTicketOwner(HttpServletRequest request,
HttpServletResponse response, String tOW) {
tOW=request.getParameter("ticketOwner");
ticketOwner1 = tOW;
}
public String getTicketOwner(){
return ticketOwner1;
}
public void setTicketTitle(HttpServletRequest request,
HttpServletResponse response, String tTI) {
tTI=request.getParameter("ticketTitle");
ticketTitle1 = tTI;
}
public String getTicketTitle(){
return ticketTitle1;
}
public void setTicketHandler(HttpServletRequest request,
HttpServletResponse response, String tHA) {
tHA=request.getParameter("ticketHandler");
ticketHandler1 = tHA;
}
public String getTicketHandler(){
return ticketHandler1;
}
public void setTicketCategory(HttpServletRequest request,
HttpServletResponse response, String tCA) {
tCA=request.getParameter("ticketCategory");
ticketCategory1 = tCA;
}
public String getTicketCategory(){
return ticketCategory1;
}
public void setTicketDescription(HttpServletRequest request,
HttpServletResponse response, String tDE) {
tDE=request.getParameter("ticketDescription");
ticketDescription1 = tDE;
}
public String getTicketDescription(){
return ticketDescription1;
}
public void setTicketDate(HttpServletRequest request,
HttpServletResponse response, String tDA) {
tDA=request.getParameter("ticketDate");
ticketDate1 = tDA;
}
public String getTicketDate(){
return ticketDate1;
}
}//GetTicketInfo
WriteToDatabase
package com.issue.handler;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WriteToDatabase {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
GetTicketInfo myGetTicketInfo = new GetTicketInfo();
try( PrintWriter out = response.getWriter()) {
String host = "jdbc:derby://localhost:1527/school_server";
String uName = "testuser";
String uPass = "testuser";
Connection con = DriverManager.getConnection(host, uName, uPass);
String query = "INSERT INTO issues (ticketID, ticketOwner, ticketTitle, ticketHandler, ticketCategory, ticketDescription, ticketDate)"+ "VALUES(?,?,?,?,?,?,?)";
PreparedStatement preparedStmt = con.prepareStatement (query);
preparedStmt.setString(1,myGetTicketInfo.getTicketID());
preparedStmt.setString(2,myGetTicketInfo.getTicketOwner());
preparedStmt.setString(3,myGetTicketInfo.getTicketTitle());
preparedStmt.setString(4,myGetTicketInfo.getTicketHandler());
preparedStmt.setString(5,myGetTicketInfo.getTicketCategory());
preparedStmt.setString(6,myGetTicketInfo.getTicketDescription());
preparedStmt.setString(7,myGetTicketInfo.getTicketDescription());
preparedStmt.execute();
}catch (Exception e) {
System.err.println("Got an exception!");
System.err.println(e.getMessage());}
}
}
索引
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Issue Handler</title>
</head>
<body>
<h1>Issue Handler</h1>
<FORM ACTION="issueHandler" method="get">
Ticket ID:
<INPUT TYPE="TEXT" NAME="ticketID" VALUE=""><BR>
Ticket Owner:
<INPUT TYPE="TEXT" NAME="ticketOwner" VALUE=""><BR>
Ticket Title:
<INPUT TYPE="TEXT" NAME="ticketTitle" VALUE=""><BR>
Ticket Handler:
<INPUT TYPE="TEXT" NAME="ticketHandler" VALUE=""><BR>
Ticket Category:
<INPUT TYPE="TEXT" NAME="ticketCategory" VALUE=""><BR>
Ticket Description:
<INPUT TYPE="TEXT" NAME="ticketDescription" VALUE=""><BR>
Ticket Date:
<INPUT TYPE="TEXT" NAME="ticketDate" VALUE=""><BR>
<INPUT TYPE="SUBMIT" VALUE="Register Ticket">
</FORM>
</body>
</html>
的web.xml
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Issue Handler</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
答案 0 :(得分:0)
您尚未提供HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
的覆盖。这是Servlet容器使用的方法。
您已提供
public void doGet(HttpServletRequest request,
HttpServletResponse response, String tID) {
tID=request.getParameter("ticketID");
ticketID1=tID;
}
这是一种完全不相关的方法。
提示:使用@Override
注释您认为自己覆盖的方法。