problem with my login servlet page
I'm facing problem with my website where I try to let the user insert the username and password to login to home page, but it show me this error
HTTP Status 404-The requested resource () is not available
It's seems that the html page does not get any result from the servlet. I'm not sure but can anybody help me and tell me what I am missing?
Here is my 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>开发者_StackOverflow;");
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";
}
}
That error just means that there's nothing which is listening on the URL.
So, either the URL is plain wrong (case sensitive!), or the Servlet isn't listening on that URL at all, or the Servlet failed to startup. Hard to tell based on the as far given information. You need to verify the URL in browser address bar, the servlet mapping in web.xml
and the server startup logs.
Unrelated to the concrete problem, assigning expensive DB resources like Connection
and on as instance variable of a servlet is an extremely bad idea. You should open and close those resources in the shortest possible scope inside the same method.
精彩评论