2013-06-02 33 views
0

你好我试图从servlet传递数据到jsp页面,但得到一个null。 请告诉我哪里出错了? 这里是我的代码:HttpServletRequest request.getAttribute()在jsp页面上为空

MyConnection.java

  public void doGet (HttpServletRequest inRequest, 
             HttpServletResponse outResponse) throws ServletException, 
             IOException { 
  
             inRequest.setAttribute ("ST", "QWERTY"); 
             RequestDispatcher dispatcher = inRequest.getRequestDispatcher ("/index.jsp"); 
  
             out.print ("\ n" + dispatcher); 
  
             dispatcher.forward (inRequest, outResponse); 
} 

的index.jsp

<% @ page contentType = "text/html" pageEncoding = "UTF-8"%> 
<% @ page language = "java" import = "java.util. *"%> 
<! DOCTYPE html> 
<html> 
     <head> 
         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
         <title> JSP Page </ title> 
     </ head> 
     <body> 
         <form action="MyConnection" name = "myForm" method="Get"> 
             <% = (String) request.getAttribute ("ST")%> 
             <% out.println (request.getAttribute ("ST"));%> 
         </ form> 
     </ body> 
</ html> 
+2

您可能完全不调用该servlet,并直接转到JSP。检查浏览器地址栏中的地址。顺便说一下,servlet中JSP的路径可能是错误的:我怀疑JSP文件名以空格开头。另外,忘记关于脚本。使用JSP EL和JSTL:'$ {ST}'。 –

+0

文件名不以空格开头,这是copy-pase错误。 地址在浏览器旁边:“http:// localhost:8080/WebApplication1/index.jsp” 我需要用过的servlets,这是一个任务) – user2430023

回答

0

在浏览器的地址是JSP的地址,所以执行JSP。要执行该servlet,必须将该servlet的地址放在地址栏中。