首页 新闻 会员 周边

form的action如何设置才能访问到servlet

0
悬赏园豆:5 [已解决问题] 解决于 2016-08-13 15:34

先贴上代码:

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错误。

请大神分析一下!!

跪谢~~~

xingzhui的主页 xingzhui | 初学一级 | 园豆:199
提问于:2016-08-07 14:21
< >
分享
最佳答案
0

今天修改了一下buildpath,设置编译的类放在web-info下的classes里面,突然就可以了~~~

xingzhui | 初学一级 |园豆:199 | 2016-08-13 15:33
其他回答(2)
0
需要设置web.xml
收获园豆:2
linquande | 园豆:204 (菜鸟二级) | 2016-08-08 00:33

我发现web.xml文件中没有任何servlet的内容,但是servlet仍然能够单独运行,只是从jsp没法访问。这是什么原因呢?

支持(0) 反对(0) xingzhui | 园豆:199 (初学一级) | 2016-08-08 07:33
0

action如何设置?找路由呗

收获园豆:3
顾晓北 | 园豆:10844 (专家六级) | 2016-08-08 10:45

什么意思大神

支持(0) 反对(0) xingzhui | 园豆:199 (初学一级) | 2016-08-08 11:29

@xingzhui: 你不是问怎么访问嘛,我虽然没做过Java,但东西是相通的,Controller/Action大致是这样子吧,Java就根据路由设置(就是相关的路由配置),找怎么访问呗

支持(0) 反对(0) 顾晓北 | 园豆:10844 (专家六级) | 2016-08-08 11:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册