HttpServletRequest request.getAttribute()在jsp页面上给出null

时间:2013-06-02 12:32:38

标签: java-ee servlets

您好我正在尝试将数据从servlet传递到jsp页面,但得到一个null。 请告诉我哪里出错了? 这是我的代码:

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>

1 个答案:

答案 0 :(得分:0)

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