用的是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的字符串没有传过来。帮我看看是哪里出了问题,是不是包导错了还是其他的代码写错了。