我的网站遇到问题,我试图让用户插入用户名和密码登录主页,但它显示我的错误
HTTP状态404-请求的资源()不可用
似乎html页面没有从servlet获得任何结果。我不确定,但有人可以帮助我并告诉我我错过了什么吗?
这是我的servlet:
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class enter extends HttpServlet {
private Connection myCon;
private PreparedStatement myStmt;
@Override
public void init() {
System.out.println("init");
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception e) {
}
try {
System.out.println("connecting");
myCon = DriverManager.getConnection ("jdbc:mysql://localhost/auk","root","password");
}
catch (SQLException e) {
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
ResultSet result;
PrintWriter out = response.getWriter();
String query, colName, dat, user;
int numCols, index;
ResultSetMetaData resultMd;
query = "Select Pass from user where Username=?";
try {
myStmt = myCon.prepareStatement(query);
}
catch (Exception e) {
}
user=request.getParameter("user");
myStmt.setString(1, user);
result=myStmt.executeQuery();
String userpass=result.getString(1);
out.println ("userpass selected from the table is = "+userpass);
response.setContentType("text/html");
out.println("<html>");
out.println("<head><title>JDBCServlet</title></head>");
out.println("<body>");
out.print("<p><b>The query is: </b>" + query + "</p>");
try {
result = myStmt.executeQuery(query);
resultMd = result.getMetaData();
numCols = resultMd.getColumnCount();
out.println("<table border>");
out.println("<caption> <b> Query Results </b> </caption>");
out.println("<tr>");
out.print("<th> Welcome " + user + " </th>");
for (index = 1; index <= numCols; index++) {
colName = resultMd.getColumnLabel(index);
out.print("<th>" + colName + "</th>");
}
out.println("</tr>");
while (result.next()) {
out.println("<tr>");
for (index = 0; index < numCols; index++) {
dat = result.getString(index + 1);
out.println("<td>" + dat + "</td>");
}
out.println("</tr>");
}
out.println("</table>");
}
catch (Exception e) {
e.printStackTrace();
}
out.println("</body></html>");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(enter.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(enter.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}
}
答案 0 :(得分:2)
该错误仅表示没有任何内容正在侦听URL。
因此,URL是完全错误的(区分大小写!),或者Servlet根本没有侦听该URL,或者Servlet无法启动。根据迄今给定的信息很难说出来。您需要验证浏览器地址栏中的URL,web.xml
中的servlet映射以及服务器启动日志。
无关,将像Connection
这样昂贵的数据库资源分配为servlet的实例变量是非常糟糕的主意。您应该在同一方法中以尽可能短的范围打开和关闭这些资源。