HttpServlet和JSP集成 [英] HttpServlet and JSP integration

查看:55
本文介绍了HttpServlet和JSP集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将自定义servlet逻辑与.jsp模板视图集成.例如,我有以下servlet:

I'm wondering is possible to integrate custom servlet logic with .jsp template view. For example I have the following servlet:

public class MyServlet extends HttpServlet {

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
      String name = "Mark";        
  }
}

,我想将name变量放在jsp文件(new.jsp)中,例如:

and I want to place name variable inside jsp file(new.jsp) like:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>New</title>
    </head>
    <body>
         <%= name %>

    </body>
</html>

我的web.xml:

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <jsp-file>/new.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/new</url-pattern>
</servlet-mapping>

我不想在请求中放入name.

有帮助吗?

更新

非常感谢,但我仍然遇到麻烦. 首先,我更新了我的servlet:

Great thanks, but I'm still having a trouble. Firstly, I updated my servlet:

public class MyServlet extends HttpServlet {

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
      String name = "Mark";
      request.setAttribute("name", name);
      request.getRequestDispatcher("/WEB-INF/new.jsp").forward(request, response);       
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
      String name = "Mark";
      request.setAttribute("name", name);
      request.getRequestDispatcher("/WEB-INF/new.jsp").forward(request, response);       
  }
}

我也改变了看法:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>New</title>
    </head>
    <body>
         ${name}

    </body>
</html>

但是当我使用${name}时,没有任何显示.我以为我应该导入任何jstl,但是不幸的是,如果我使用<%= request.getAttribute("name") %>,我会得到null.

But when I use ${name} there's nothing displayed. I thought that I should import any jstl, but unfortunately if I use <%= request.getAttribute("name") %> I'm getting null.

更新2 终于解决了!是我的错,我忘了设置

UPDATE 2 Finally solved! It was my fault, I forgot set

<servlet>
   <servlet-name>MyServlet</servlet-name>
   <servlet-class>com.example.MyServlet</servlet-class>
</servlet>

推荐答案

您需要改为实现doGet()方法.普通的HTTP请求(单击链接,书签或直接在浏览器地址栏中输入URL)默认为GET方法.

You need to implement the doGet() method instead. A normal HTTP request (clicking a link, a bookmark or entering the URL straight in browser address bar) defaults to GET method.

为了使对象在预处理servlet中的JSP中可用,您需要将其设置为请求,会话或应用程序范围内的属性.最后,您需要将请求/响应转发到JSP,以便可以显示它.

In order to make objects available in JSP in a preprocessing servlet, you need to set it as an attribute in request, session or application scope. Finally you need to forward the request/response to the JSP so that it can be displayed.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name = "Mark";
    request.setAttribute("name", name);
    request.getRequestDispatcher("/WEB-INF/new.jsp").forward(request, response);
}

如果您按照以下步骤修复servlet映射

If you fix the servlet mapping as follows

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.example.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/new</url-pattern>
</servlet-mapping>

然后,您可以通过 http://localhost:8080/contextname/new 来调用servlet. .在转发的JSP中,您可以仅通过EL

then you can just invoke the servlet by http://localhost:8080/contextname/new. In the forwarded JSP you can access the name just by EL

${name}

请注意,JSP放置在/WEB-INF文件夹中,以避免直接访问,而无需通过在浏览器地址栏中输入JSP URL来调用预处理servlet.

Note that the JSP is placed in /WEB-INF folder in order to avoid direct access without the preprocessing servlet being called by entering the JSP URL in browser address bar instead.

  • Our Servlets wiki page
  • Our Expression Language wiki page
  • How to avoid Java code in JSP files?

这篇关于HttpServlet和JSP集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆