求救,初学servlet遇到的问题

ivent1314 2013-03-13 09:34:59
这是该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的错误。求大神指导!
...全文
352 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kissoday 2013-03-14
  • 打赏
  • 举报
回复
你的URL配置有问题吧。。 把你的那个servlet的类放到一个包里,然后重新配置下web.xml中servlet的URL 把Tomcat的临时文件清理一下,重新加载下应该就能解决问题了。 遇到问题应该了解问题本质。 404错误就是系统找不到文件路径。知道问题根本,解决问题也会变得容易的多。
ivent1314 2013-03-14
  • 打赏
  • 举报
回复
引用 10 楼 yulongde77 的回复:
初学一定要仔细,一步一个脚印,要熟悉流程。 out.println("<h1 align=center>欢迎第一次使用Servlet</h1>"); out.println("<form method=post action=/ServletModule/loginservlet"); out.println("<br/>"); ……
好了,谢谢!
GreyZeng 2013-03-14
  • 打赏
  • 举报
回复
web.xml可能没有配置对
  • 打赏
  • 举报
回复

在web.xml里配置

<servlet>
    <servlet-name>ClientServlet</servlet-name>
    <servlet-class>hello.ClientServlet</servlet-class>
  </servlet>
 
 
   
  <servlet-mapping>
    <servlet-name>ClientServlet</servlet-name>
    <url-pattern>/ClientServlet</url-pattern>
  </servlet-mapping>
 
General-M 2013-03-14
  • 打赏
  • 举报
回复
上web.xml
百八烦恼风 2013-03-14
  • 打赏
  • 举报
回复
404那就是没找到咯,看看配置咯,
wangjiangbo2 2013-03-14
  • 打赏
  • 举报
回复
404错误,web.xml设置servlet指向,你没有调用对。
ivent1314 2013-03-14
  • 打赏
  • 举报
回复
是的 out.println("<form method=post action=/ServletModule/loginservlet");是这个错误了,没有该路径,应该如何该才能回到doget()呢
海逸_2018 2013-03-14
  • 打赏
  • 举报
回复
初学一定要仔细,一步一个脚印,要熟悉流程。
out.println("<h1 align=center>欢迎第一次使用Servlet</h1>");
out.println("<form method=post action=/ServletModule/loginservlet");
out.println("<br/>");

这里的action路径从哪里来的呢?
ivent1314 2013-03-14
  • 打赏
  • 举报
回复
不显示哪个地方错误
ivent1314 2013-03-14
  • 打赏
  • 举报
回复
<servlet> <servlet-name>ClientServlet</servlet-name> <servlet-class>hello.ClientServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ClientServlet</servlet-name> <url-pattern>/ClientServlet</url-pattern> </servlet-mapping> 已经改成这样了,但还是显示错误,而且myEclipse又不显示错误
IMBA__小八 2013-03-13
  • 打赏
  • 举报
回复
1.Web.xml里面有没有配置servlet 2.看你的url有没有被servlet拦截到

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧