简体   繁体   English

从html访问WEB-INF / jsp

[英]access WEB-INF/jsp from html

I have a simple application to test the communication between html and jsp. 我有一个简单的应用程序来测试html和jsp之间的通信。 My jsp is located in 我的jsp位于

WEB-INF/test.jsp

Here is the structure of my files: 这是我文件的结构:

ProjectA 
  src 
    irstServlet.java 
  Web-Content 
    test1.html 
    WEB-INF 
      test.jsp 

Here is the code from servlet 这是来自servlet的代码

protected void doPost(HttpServletRequest request, response) throws ServletException, IOException {
  RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/test.jsp");
  request.setAttribute("userName", request.getParameter("userName"););
  dispatcher.forward(request, response);
}

First I have deploy in tomcat start my test1.html : It take me to the servlet: FirstServlet.java and I can enter userName there. 首先,我已经部署在tomcat中,启动了test1.html :将我带到Servlet: FirstServlet.java然后可以在其中输入userName。

But after i enter the values in and press enter I expect it to forward me to test.jsp which is not working. 但是在我输入值并按Enter之后,我希望它可以将我转发到不起作用的test.jsp I get the error: 我收到错误:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. 原始服务器未找到目标资源的当前表示,或不愿意透露该资源的存在。

Edited: In my html I am trying to use it like: 编辑:在我的html中,我试图像这样使用它:

<form method="POST" name="XX" action="/HelloWorldServlet">

Still not working. 还是行不通。

Please can someone help me? 有人可以帮我吗?

Your code does not look like it would compile at all. 您的代码看起来根本无法编译。

  • Parameter response has no type - should be HttpServletResponse 参数response没有类型-应该是HttpServletResponse
  • There is a semicolon ( ; ) after request.getParameter("userName") request.getParameter("userName")后有一个分号( ; request.getParameter("userName")

Also I'm not sure why you're getting RequestDispatcher from servlet context rather than from the request - then again I've never checked if it makes any difference. 另外,我不确定您为什么从Servlet上下文而不是从请求中获取RequestDispatcher然后我再也没有检查过它是否有任何区别。

Anyway, I would rewrite doPost method like this: 无论如何,我会这样重写doPost方法:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setAttribute("userName", request.getParameter("userName"));
    req.getRequestDispatcher("/WEB-INF/test.jsp").forward(req, resp);
}

EDIT: 编辑:

I'm assuimng you have either a correct servlet mapping in your web.xml: 我保证您的web.xml中有正确的servlet映射:

<servlet>
    <servlet-name>HelloWorldServlet</servlet-name>
    <servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>

or your servlet is annotated with @WebServlet annotation: 或用@WebServlet注释对servlet @WebServlet注释:

@WebServlet("/HelloWorldServlet")
public class FirstServlet extends HttpServlet {
    //your code
}

. If neither of those is true, that's your problem right there. 如果都不是真的,那就是您的问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM