【问题标题】:Pass parameters from servlet to jsp using HTTPServletRequest return null [duplicate]使用 HTTPServletRequest 将参数从 servlet 传递到 jsp 返回 null [重复]
【发布时间】:2016-03-27 10:21:33
【问题描述】:

我正在尝试使用 HTTPServletRequest 将参数从 servlet 传递到 JSP,但没有成功。谁能解释我哪里错了?

这里是 servlet: HelloServlet

package net.codejava;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class HelloServlet
 */
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

        request.setAttribute("name", "name1");
        request.setAttribute("surname", "surname1");
        request.getRequestDispatcher("index.jsp").forward(request, response);

    }

}

这里是 JSP: JSP(“HelloServlet/index.jsp”)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HelloWorld Index</title>
</head>
<body>
    Welcome <%= request.getAttribute("name") %>
</body>
</html>

和 web.xml: web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>helloservlet</servlet-name>
        <servlet-class>net.codejava.HelloServlet</servlet-class>
        <jsp-file>/HelloServlet/index.jsp</jsp-file>        
    </servlet>
    <servlet-mapping>
        <servlet-name>helloservlet</servlet-name>
        <url-pattern>/HelloServlet</url-pattern>
    </servlet-mapping>
</web-app>

结果

Welcome null

【问题讨论】:

  • 尝试使用 request.getSession().setAttribute 和 request.getSession().getAttribute
  • @NishadKAhamed 多么随机的解决方案。不要只是抛出解决方案。你知道当前行为的原因吗?
  • @sᴜʀᴇsʜ ᴀᴛᴛᴀ 这不是一个解决方案,我只是提出一个建议来检查它是否有效。这就是为什么我只是发表评论而不是将其发布为答案。另外,请尝试思考一个问题的答案,而不是浪费时间寻找错误。
  • @NishadKAhamed 不,它不起作用。
  • @NishadKAhamed 我什么也不说,如果你确定“发现错误是浪费时间”

标签: java jsp servlets web.xml


【解决方案1】:

实际上,您的操作永远不会被调用。 尝试更改代码中的以下部分。

更新web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>helloservlet</servlet-name>
        <servlet-class>net.codejava.HelloServlet</servlet-class>
   </servlet>
    <servlet-mapping>
        <servlet-name>helloservlet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
</web-app>

并通过

调用您的操作
http://localhost:8080/HelloServlet/index.htm

【讨论】:

  • 为什么我需要调用htm文件而不是jsp?
  • 它不是一个文件,只是一个操作路径。您可以将其更改为 web.xml url 映射中的任何值。它只是一个示例。
猜你喜欢
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多