2014-09-03 105 views
-1

我的測試頁很少,但是我無法獲得請求參數,也不明白爲什麼。 測試頁具有的 「href」 鏈接是這樣的:如何處理href參數jsp

<a href="main.jspc?param=show" class="submenu">Show smth</a> 

簡單的控制器:

import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 

public class Controller extends HttpServlet { 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     processRequest(request, response); 
    } 

    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     processRequest(request, response); 
    } 

    protected void processRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     StringBuilder sb = new StringBuilder("Do SMTH<br>"); 
     sb.append("Other will be later...<br>"); 
     String s = sb.toString(); 
     request.setAttribute("result", s); 
     RequestDispatcher view = request.getRequestDispatcher("test.jsp"); 
     view.forward(request,response); 
    } 
} 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 
    <welcome-file-list> 
     <welcome-file>main.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
     <servlet-name>controller</servlet-name> 
     <servlet-class>Controller</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>controller</servlet-name> 
     <url-pattern>/main.jspc</url-pattern> 
    </servlet-mapping> 
</web-app> 

和測試頁面,在這裏我想說明一些獲得參數:

test.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
<% 
    out.print("<br>" + "Parameter: "); 
    request.getParameter("param"); 
    out.print("<br>"+ "URL:"); 
    request.getRequestURL(); 
    out.print("<br>" + "Result:"); 
    request.getParameter("result"); 
%> 
</body> 
</html> 

但測試頁顯示空參數,我不明白爲什麼,所以我需要一點幫助。

+0

嘗試使用'$ {} param.param'和'$ {結果}'上test.jsp的 – Arvind 2014-09-03 12:22:54

+0

可以看到Show smth。我在這裏看到jspc.can,你試着把它改成jsp – KVK 2014-09-03 12:25:40

+0

它會是完全正確的版本嗎? – 2014-09-03 12:27:11

回答

0

自從taglibs(JSTL)和EL(Expression Language,這些$ {}事物)誕生以來,JSP中使用scriptlet(那些東西)是舊的並且令人沮喪。

使用下面的代碼訪問您的參數:

Parameter : ${params} <br> 
Result : ${result} <br> 
URL : ${pageContext.request.requestURI}