先贴上代码:
servlet的:
1 package chapter10; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 /** 13 * Servlet implementation class DoGetDemo 14 */ 15 @WebServlet("/DoGetDemo") 16 public class DoGetDemo extends HttpServlet { 17 private static final long serialVersionUID = 1L; 18 19 /** 20 * @see HttpServlet#HttpServlet() 21 */ 22 public DoGetDemo() { 23 super(); 24 // TODO Auto-generated constructor stub 25 } 26 27 /** 28 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 29 */ 30 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 31 // TODO Auto-generated method stub 32 response.setContentType("text/html;charset=gb2312"); 33 request.setCharacterEncoding("utf-8"); 34 PrintWriter out=response.getWriter(); 35 String username=request.getParameter("username"); 36 String password=request.getParameter("password"); 37 out.println("<html>"); 38 out.println("<body>"); 39 out.println("用户名:"+username+"<br>"); 40 out.println("用户密码:"+password); 41 out.println("</body>"); 42 out.println("</html>"); 43 } 44 45 }
jsp页面的:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8" import="java.util.*" %> 3 <%@ page import="java.sql.*" %> 4 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 5 <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 10 <title>用户表达</title> 11 </head> 12 <body> 13 <center> 14 <form action="DoGetDemo" method="get"> 15 用户名:<input type="text" name="username"/><br> 16 密码:<input type="password" name="password"/><br> 17 <input type="submit" value="submit"/> 18 <input type="reset" value="reset"/> 19 </form> 20 </center> 21 </body> 22 </html>
我的servlet是通过Eclipse的新建servlet建立的,不知道是否还需要设置web.xml?
最后贴上结构图:
现在单独运行servlet可以;运行jsp页面可以;但是一点提交就出现404错误。
请大神分析一下!!
跪谢~~~
今天修改了一下buildpath,设置编译的类放在web-info下的classes里面,突然就可以了~~~
我发现web.xml文件中没有任何servlet的内容,但是servlet仍然能够单独运行,只是从jsp没法访问。这是什么原因呢?
action如何设置?找路由呗
什么意思大神
@xingzhui: 你不是问怎么访问嘛,我虽然没做过Java,但东西是相通的,Controller/Action大致是这样子吧,Java就根据路由设置(就是相关的路由配置),找怎么访问呗