Multiple folders in Java EE 6 web pages

一个人想着一个人 提交于 2019-12-12 18:28:06

问题


Lets say that you have a following simple application:

<form action="helloServlet" method="post">

        Give name:<input type="text" name="name" />

        <input type="submit" value="Send"/>
</form>

And a servlet handling that form:

package org.servlets.hello;

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(name="helloServlet", urlPatterns={"/helloServlet"})
public class helloServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("name");


    PrintWriter out = response.getWriter();

    try {

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet helloServlet</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello  " + name + "</h1>");
        out.println("</body>");
        out.println("</html>");

    } finally { 
        out.close();
    }
} 

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
}

}

Now this works perfectly when it is ran from a web page located at the "Web Pages" folder but when I try to do the same in a subfolder in Web Pages I am given 404 error (The requested resource () is not available.). Am I supposed to change something to make the subfolder a valid location to call for a servlet?


回答1:


If the POST action url starts with "/" it is absolute. Or else, it is relative to the current page

Because the post url <form action="helloServlet" , helloServlet is relative to the current path.

This is how the helloservlet url will be accessed:

          Current Page          Servlet URL
Usage 1   /form.html            /helloservlet
Usage 2   /subfolder/form.html  /subfolder/helloservlet

And in your case, the Usage 1 works because Servlet is mapped to /helloservlet.

Solution:

Modify line

<form action="helloServlet" method="post">

to

<form action="/{contextPath}/}helloServlet" method="post">

I think your context path is not default "/" context path. In that case, you should be having url relative to the domain. Replace {contextpath} with the actual context path.




回答2:


Alright I figured it out myself. Here's my solution in case someone else runs into same problem. As I understand it the standard java web project folder structure is as follows:

  • Web Pages
    • WEB-INF
    • SUBFOLDER
  • Source Packages
    • org.whatever.name
  • Libraries
  • Configuration files

As such if we have a hello.jsp in a SUBFOLDER where we have a form with the following:

<form action="helloServlet" method="post">

And a servlet in org.whatever.name with such:

@WebServlet(name="helloServlet", urlPatterns={"/helloServlet"})

Then the servlet location is mapped based on the Web Pages folder and the two files cannot find each other. So in order to make the servlet available to subfolders then we either change the servlet:

@WebServlet(name="helloServlet", urlPatterns={"SUBFOLDER/helloServlet"})

or the form:

<form action="../helloServlet" method="post">



回答3:


Use a domain-relative URL.

<form action="${pageContext.request.contextPath}/helloServlet">

No need to repeat every single folder in servlet mapping.

See also:

  • Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP


来源:https://stackoverflow.com/questions/11106130/multiple-folders-in-java-ee-6-web-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!