jsp頁面中獲取servlet請求中的參數的辦法詳解
更新時間:2018年03月18日 17:41:12 投稿:wdc
在JAVA WEB應用中,如何獲取servlet請求中的參數,本文講解了jsp頁面中獲取servlet請求中的參數的辦法
在JAVA WEB應用中,如何獲取servlet請求中的參數,并傳遞給跳轉的JSP頁面?例如訪問http://localhost:8088/bbs?id=1
當執(zhí)行這個bbs servlet時,將url參數id的值傳遞給bbs.jsp頁面?
1.首先要配置web.xml,見下面的配置:
<servlet> <servlet-name>bbs</servlet-name> <servlet-class> org.openjweb.core.servlet.BBSServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>bbs</servlet-name> <url-pattern>/bbs</url-pattern> </servlet-mapping>
2.編寫servlet類:
package org.openjweb.core.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BBSServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public BBSServlet()
{
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
//http://bbs.csdn.net/topics/90438353
request.setCharacterEncoding("UTF-8"); //設置編碼
String id = request.getParameter("id");
request.setAttribute("id", id);
request.getRequestDispatcher("/bbs.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
在應用根目錄創(chuàng)建bbs.jsp文件,內容為:
<%@ page contentType="text/html;charset=UTF-8"%>
<%
out.println(request.getAttribute("id"));
%>
注意很多人傳遞參數不成功是因為是在doGet方法中調用doPost,這里doGet方法不要調用doPost.
相關文章
SpringBoot前后端分離解決跨域問題的3種解決方案總結
前后端分離大勢所趨,跨域問題更是老生常談,下面這篇文章主要給大家介紹了SpringBoot前后端分離解決跨域問題的3種解決方案,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-05-05
spring security結合jwt實現用戶重復登錄處理
本文主要介紹了spring security結合jwt實現用戶重復登錄處理,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
詳解Java編程中包package的內容與包對象的規(guī)范
這篇文章主要介紹了Java編程中包package的內容與包對象的規(guī)范,是Java入門學習中的基礎知識,需要的朋友可以參考下2015-12-12

