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>
您可能完全不調用該servlet,並直接轉到JSP。檢查瀏覽器地址欄中的地址。順便說一下,servlet中JSP的路徑可能是錯誤的:我懷疑JSP文件名以空格開頭。另外,忘記關於腳本。使用JSP EL和JSTL:'$ {ST}'。 –
文件名不以空格開頭,這是copy-pase錯誤。 地址在瀏覽器旁邊:「http:// localhost:8080/WebApplication1/index.jsp」 我需要用過的servlets,這是一個任務) – user2430023