我正在尝试将注册信息链接到数据库mysql。当我将数据导出到文本文件时,一切正常,但是当尝试连接数据库时,它总是会出现此错误。这是我到目前为止尝试的代码: Register.jsp:
enter code here
<form action="./Data">
<table>
<tr>
<td> Username </td>
<td><input type ="text" name="uname" size="30"required></td>
</tr>
<tr>
<td>Your email address </td>
<td><input type ="text" name="mail" size="30"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" size="20"required></td>
</tr>
<tr>
<td>Please re-type your password</td>
<td><input type="password" name="p2" size="20"required></td>
</tr>
<table>
<tr>
<td>Date of Birth</td>
<td><select name="Type">
<%for(int i=1;i<=31;i++) {%>
<option><%=i%></option>
<%}%>
</select></td>
<td>
<select name="Type">
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
</td>
<td>
<select name="Type">
<% for (int j=1970;j<=2010;j++) {%>
<option><%=j%></option>
<%}%>
</select>
</td>
<tr>
<td>Gender</td>
<td><input type ="radio" value="r1" checked name="Gender">Male</td>
<td><input type ="radio" value="r2" checked name="Gender">Female</td>
</tr>
<tr>
<td>
</tr>
<tr>
<td><input type="submit" value="Register" name="s4"></td>
<td><input type="reset" value="Reset" name="s5"></td>
</tr>
</table>
</form>
enter code here
Data.java:
enter code here
@WebServlet(urlPatterns = {"/Data"})
public class Data extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String user = request.getParameter("uname");
String pass = request.getParameter("pass");
String email = request.getParameter("mail");
String gender = request.getParameter("Gender");
String end = ("----------------------");
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String connectionURL = "jdbc:mysql://localhost:8084/userinfo";
Connection con = DriverManager.getConnection(connectionURL,"root","");
Statement st = con.createStatement();
ResultSet rs;
int i = st.executeUpdate("insert into userinfo (Username, email, password) values('" + user + "','" + pass + "','" + email + "')");
if (i > 0) {
response.sendRedirect("welcome.jsp");
out.print("Registration Successfull!"+"<a href='index.jsp'>Go to Login</a>");
} else {
response.sendRedirect("index.jsp");
}
response.sendRedirect("welcome.jsp");
}
}
答案 0 :(得分:0)
您的Data
类扩展了HttpServlet
,实现了doGet()
方法。
当Servlet
容器收到任何请求时,它会调用您的Servlet'
service()
方法。在这种情况下,这是调用HttpServlet#service()
方法的继承HttpServlet#doGet()
方法。由于您尚未实现该方法,因此您的类将继承该方法。在HttpServlet
类中,它实现如此
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
因此,当您发送GET请求时,将调用此方法,您将获得405响应代码。
实施您自己的doGet()
代理processRequest
。
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
processRequest(req, resp);
}