Servlet如何获取url中的参数呢?
下文讲述在Servlet doGet请求中获取url中参数的方法分享,如下所示:
实现思路: 使用request.getParameter("name") 即可获取url中参数值 注意事项: 参数名称在Servlet中是区别大小写的例: 以下是笔者在Servlet helloWorld程序上的扩展
package servlet01; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/helloWorld") public class ServletHelloWorld extends HttpServlet { /** * */ private static final long serialVersionUID = 888; private String msg; @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); msg = "java265.com is Java WebSite."; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub resp.setContentType("text/html"); String urlInfo = ""; if (req.getParameter("urlInfo") != null) { urlInfo = new String(req.getParameter("urlInfo").getBytes("ISO-8859-1"), "UTF-8"); } // 输出获取的值 PrintWriter o = resp.getWriter(); o.println("<h4>" + msg + "</h4>"); o.println("<h5>GetParameter:" + urlInfo + "</h5>"); } }运行效果如下所示:
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。