十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在jsp頁面中獲取servlet請求的參數(shù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1.首先要配置web.xml,見下面的配置:
bbs org.openjweb.core.servlet.BBSServlet bbs /bbs
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"); //設(shè)置編碼 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); } }
在應(yīng)用根目錄創(chuàng)建bbs.jsp文件,內(nèi)容為:
<%@ page contentType="text/html;charset=UTF-8"%> <% out.println(request.getAttribute("id")); %>
注意很多人傳遞參數(shù)不成功是因?yàn)槭窃赿oGet方法中調(diào)用doPost,這里doGet方法不要調(diào)用doPost.
上述就是小編為大家分享的怎么在jsp頁面中獲取servlet請求的參數(shù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。