servlet传值到jsp页面,jsp中得到的值为空,请教大神【】

qq_32747717 2016-04-13 05:30:39
用的是myeclipse创建的web工程,以下是整个web工程的结构:



实现的功能是将servlettojsp.java这个servlet中的字符串,通过request.setattribute()传到index.jsp页面中。

下面是servlettojsp.java的源代码

package com.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlettojsp extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s="sssss";
request.setAttribute("ss", s);
request.getRequestDispatcher(request.getContextPath()+"index.jsp").forward(request, response);

}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doGet(request, response);
}

}


下面是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">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>servlettojsp</servlet-name>
<servlet-class>com.test.servlettojsp</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>servlettojsp</servlet-name>
<url-pattern>/servlet/servlettojsp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


下面是index.jsp中的全部代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
This is my JSP page. <br>
http://blog.csdn.net/ssy_shandong/article/details/9328985
<%
String ssa=(String)request.getAttribute("ss");

%>
<p>
The String is:<%=ssa %>
</p>
</body>
</html>

最后运行的结果显示为:



最下面一行的最后有一个null值,说明servlet的字符串没有传过来。帮我看看是哪里出了问题,是不是包导错了还是其他的代码写错了。
...全文
766 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
我也是,结果一看,我也运行成了jsp页面,感谢。
奄灬苟且偷生 2016-04-13
  • 打赏
  • 举报
回复
工程名自己改一下
奄灬苟且偷生 2016-04-13
  • 打赏
  • 举报
回复
你直接请求的jsp页面 能好使就怪了 你敢不敢 请求地址 http://localhost:8080/工程名/servlet/servlettojsp

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧