麻烦将URL映射到我的Java Servlet

时间:2016-05-17 14:44:43

标签: java jsp tomcat url web-applications

我需要通过使用以下模式在浏览器中输入URL来从数据库中获取对象:

https://host.com/<something>?action=<actionName>&object=<node|interface>&selection=<list>&tenants=<list>

“something”是检索参数的方法,参数包括:action,object,selection和tenants。

我如何在web.xml文件中建立模式以符合理解上述每个元素的方法?

  <servlet>
    <servlet-name>NewDynamicWebProject</servlet-name>
    <servlet-class>com.test.package.NewDynamicWebProject</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>NewDynamicWebProject</servlet-name>
    <url-pattern>/something/*</url-pattern>
  </servlet-mapping>

我的课程:

@SuppressWarnings("serial")
public class NewDynamicWebProject extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        something(req, resp);

    }

    protected void something(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String actionName = req.getParameter("action");
        PrintWriter out = resp.getWriter();
        out.print("<div>" + actionName + "</div>");

    }

}

如何确保我传递到我的网址的参数在我的“某事”方法的范围内有意义?

1 个答案:

答案 0 :(得分:0)

我放弃了“某事”课,只做了以下几点:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String actionName = req.getParameter("action");
    String objectType = req.getParameter("object");
    String selectionList = req.getParameter("selection");
    String tenantsList = req.getParameter("tenants");

    PrintWriter out = resp.getWriter();
    out.print("<div>" + actionName + " " + objectType + " " + selectionList + " " + tenantsList + "</div>");

}