我有一个将对象从servlet传递给jsp的问题。
Servlet.java
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Controller test = new Controller();
test.setObjects();
request.getSession().setAttribute("item", test.node_1);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
的index.jsp
<title> ${item.firstName} </title>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>socialgraphui.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/Servlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Tomcat输出
okt 25, 2014 6:17:14 PM org.apache.catalina.startup.HostConfig deleteRedeployResources
INFO: Undeploying context []
okt 25, 2014 6:17:14 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /Library/Java/Servers/apache-tomcat-7.0.42/conf/Catalina/localhost/ROOT.xml
okt 25, 2014 6:17:14 PM org.apache.catalina.util.LifecycleBase start
INFO: The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]] after start() had already been called. The second call will be ignored.
在浏览器控制台中没有错误或警告消息。能帮到我,我怎么能找出问题所在?
答案 0 :(得分:3)
检查您关注的网址应为:
http://localhost:8080/your project name/Servlet
如果item.getFirstName
返回null,您将在标签中看不到任何内容
要确保传递给jsp页面的param更改了String的item
值,例如
request.getSession.setAttribute('item' , 'my title');
现在,如果您在<title>
代码中看到我的标题,则在您的对象中传递param done和reason。
答案 1 :(得分:2)
通过在 JSP 页面上设置以下参数,确保允许 JSP 访问Session
请求对象:
<%@ page session="true" %>
为避免HTML非法字符,最好使用el:
<title>
<c:out value="${sessionScope.item.firstName}"/>
</title>
注意: 要使用c
(核心)JSTL,您必须通过在 JSP上声明下面的代码来导入标记库页面:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
答案 2 :(得分:0)
您正在会话范围中设置对象。因此,您可能希望使用sessionscope来检索值:
<title>${sessionScope.item.firstName}</title>