求救,初学servlet遇到的问题
这是该JAVA文件的源代码
package hello;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ClientServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public ClientServlet() {
super();
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void userInterface(HttpServletResponse response)throws IOException
{
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head><title>loginservlet</title></head>");
out.println("<body bgcolor="#ffffff">");
out.println("<h1 align=center>欢迎第一次使用Servlet</h1>");
out.println("<form method=post action=/ServletModule/loginservlet");
out.println("<br/>");
out.println("用户名:<input type='text' name='username'><br/>");
out.println("密码:<input type='password' name='password'><br/>");
out.println("<input type=submit value='提交'><br/>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("GB2312");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("") || password.equals("")){
userInterface(response);
}
else{
out.println("<b>"+username + "</b> 欢迎第一次使用Servlet<br/>");
out.println("<b>今天的日期是:</b>" + new Date());
}
}
catch(Exception e){
userInterface(response);
}
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
尝试了很多次,每次输入用户名和密码时都出现HTTP Status 404的错误。求大神指导! servlet
[解决办法]
1.Web.xml里面有没有配置servlet
2.看你的url有没有被servlet拦截到
[解决办法]
404错误,web.xml设置servlet指向,你没有调用对。
[解决办法]
404那就是没找到咯,看看配置咯,
[解决办法]
你的URL配置有问题吧。。
把你的那个servlet的类放到一个包里,然后重新配置下web.xml中servlet的URL
把Tomcat的临时文件清理一下,重新加载下应该就能解决问题了。
遇到问题应该了解问题本质。
404错误就是系统找不到文件路径。知道问题根本,解决问题也会变得容易的多。
[解决办法]
初学一定要仔细,一步一个脚印,要熟悉流程。
out.println("<h1 align=center>欢迎第一次使用Servlet</h1>");
out.println("<form method=post action=/ServletModule/loginservlet");
out.println("<br/>");
这里的action路径从哪里来的呢?