我正在尝试使用servlet将新用户输入我的数据库。 当我单击提交按钮i注册表单时出现问题。我得到了#34;请求的资源不可用。"错误。我无法弄明白。
你们能帮助我吗?
register.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sign Up</title>
</head>
<body>
<h1>Enter your information</h1>
<form method = "POST" action="RegisterServlet">
Username <br/>
<input type="text" name="username"/><br/>
Password <br/>
<input type="password" name="password"/><br/>
Email <br/>
<input type="text" name="email"/><br/>
First name <br/>
<input type="text" name="fname"/><br/>
Last name <br/>
<input type="text" name="lname"/><br/>
<input type="submit" value="Confirm" />
</form>
</body>
RegisterServlet doPost()方法
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "RegisterServlet", urlPatterns = {"/RegisterServlet"})
public class RegisterServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String admin = "no";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mobmelbaza", "root", "");
String query = "insert into korisnici(user, pass, email, ime, prezime, admin)"
+ "values ('" + username + "','" + password + "','" + email + "','" + fname + "','" + lname + "', '" + admin + "')";
Statement st = con.createStatement();
boolean done = st.execute(query);
if (done == true) {
out.print("Sign Up sucessfull");
} else {
out.print("Failed");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>servlet.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
答案 0 :(得分:1)
如您所说,您正在使用注释,请更好地从web.xml中删除映射 您可以使用基于注释的映射,例如
@WebServlet(name = "RegisterServlet", urlPatterns = {"/RegisterServlet"})
或
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>servlet.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
你不能同时拥有两个