6

这是我的index.jsp代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index JSP File</title>
</head>
<body>
<form action="/IndexController" method="get">
<table>
<tr><td>Enter Your Name :</td> <td><input type="text" name="name"/></td></tr>
<tr><td><input type="submit" value="Submit" /></td></tr>
</table>
</form>
</body>
</html>   

这是我的IndexControllerServlet 代码:

public class IndexController extends HttpServlet {

    protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uname = request.getParameter("name");
        response.sendRedirect("welcome.jsp?name="+uname);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doProcess(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doProcess(request, response);
    }

}

这是我的welcome.jsp页面代码

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% String fname = request.getParameter("uname");%>
<h1>Welcome to JSP World,  <%=fname%></h1>
</body>
</html>

当我通过 Java EE 运行时运行它时,我得到了一个index.jsp页面,但是在输入名称并单击提交后,我得到了以下错误:

Error 404 - Not Found No context on this server matched or handled this request. Contexts known to this server are:  JSPExample(/JSPExample)
4

3 回答 3

0

编辑 web.xml 并添加 servlet 映射

<servlet>
      <servlet-name>IndexController</servlet-name>
      <servlet-class>IndexController</servlet-class>
</servlet>
<servlet-mapping>
      <servlet-name>IndexController</servlet-name>
      <url-pattern>/IndexController</url-pattern>
</servlet-mapping>
于 2012-08-20T14:39:02.067 回答
0

还将welcome.jsp 中的行更改<% String fname = request.getParameter("**uname**");%><% String fname = request.getParameter("**name**");%>,因为您的参数名称是name response.sendRedirect("**welcome.jsp?name="+uname);**:)

于 2012-08-20T14:42:22.710 回答
0

在 Eclipse 中,如果您遇到此问题,只需使用另一个名称创建一个新的索引文件。然后终止最后运行的服务器,并启动一个新的服务器,运行新的 index.. 文件。我用这种方式解决了这个问题,最后我删除了新的索引文件并运行了我的第一个索引文件。

于 2015-04-05T20:41:52.830 回答