简体   繁体   English

如何从Java Servlet中的URL读取参数

[英]how to read parameters from URL in java servlet

i try to read the parameters from the url in the following way http://localhost:8080/nameOfmyProject/nameOfMyServlet?query=bla 我尝试通过以下方式从url中读取参数:http:// localhost:8080 / nameOfmyProject / nameOfMyServlet?query = bla

the code inside my doGet method is 我的doGet方法中的代码是

   public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

 String query = request.getParameter("query");

 response.setContentType("text/html");
 PrintWriter out = response.getWriter();

 out.println("<html>");
 out.println("the parameter is " + query);
 out.println("</html>");
 out.close();

it builds just fine but i get an status 404. Can someone please tell me what im doing wrong? 它构建的很好,但我的状态为404。有人可以告诉我即时消息做错了什么吗? thanks in advance! 提前致谢!

The request.getParameter("query") will look for the input field called query in the html form. request.getParameter("query")将以html形式查找称为query的输入字段。 If you want to get the path variable from the url you get it like this 如果您想从网址中获取路径变量,则可以像这样获取它

  String pathInfo = request.getPathInfo(); // query=bla
    String[] pathParts = pathInfo.split("=");
    String part1 = pathParts[1]; // query
    String part2 = pathParts[2]; //bla

I found the solution: it was a problem with starting the tomcat server. 我找到了解决方案:启动tomcat服务器时出现问题。 Somehow the catalina.sh had no execute permissions. catalina.sh不知何故没有执行权限。 I changed that in the cmd via: 1. i checked for permissions via ls -l 2. i changed the permissions via chmod +x catalina.sh 我通过以下方式在cmd中进行了更改:1.我通过ls -l检查了权限。我通过chmod + x catalina.sh更改了权限。

and now it works perfectly. 现在它可以完美运行了。 Thank you all! 谢谢你们!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM