2

I want a simple servlet that can be used without parameters. Something like :

http://servername:8080/do/view/username/address

and use it like a parameter :

http://servername:8080/do?action=view&login=username&page=address

Both urls will have the same behaviour. I prefer don't use any framework, only servlets and filters.

How can I obtain the url name from the servlet? What's the best solution?


Response:


Based on the reply made by @BalusC i have created the following servlet that do all i want:

@WebServlet("/do/*")
public class ActionTestCasesServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException  
{
  String pathInfo = request.getPathInfo();
  String[] parts = pathInfo.substring(1).split("/");
  RequestDispatcher destination = getServletContext()
            .getRequestDispatcher("/" + parts[0] + ".jsp");
  if (parts.length > 1) {
request.setAttribute("username", parts[1]);
  }
  if (parts.length > 2) {
    request.setAttribute("page", parts[2]);
  }
  destination.forward(request, response);

 }
}

This code call the "view.jsp" passing the attributes "username" and "page".

Community
  • 1
  • 1
F.Rosado
  • 487
  • 4
  • 20

4 Answers4

6

Just map the servlet on /do/* instead of /do.

@WebServlet("/do/*")

This way you can obtain the path information using HttpServletRequest#getPathInfo().

String pathInfo = request.getPathInfo(); // /view/username/address

You can use the usual String methods like String#split() to split it in fragments.

String[] parts = pathInfo.substring(1).split("/"); // view, username, address

See also:

  • Design Patterns web based applications - for the case you intend to homegrow MVC (note: I don't recommend this, it's fun if it's for learning/hobby purposes, but if it's for real job, rather pick an existing MVC framework so that you can ensure that every pitfall is covered)
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks @BalusC, i don't want create my homegrow MCV, i only need a page with two or three concrete actions which are easy to remember / understand. Your solution appear very useful. – F.Rosado Jun 11 '13 at 07:25
2

You say you'd prefer not to use "any framework, only servlets and filters", but have you considered the tuckey.org UrlRewriteFilter? This is a single filter that you can register in web.xml and then declare rules such as

    <rule>
        <from>/do/(.+)/(.+)/(.+)</from>
        <to>/do?action=$1&amp;login=$2&amp;page=$3</to>
    </rule>

in an XML file. Then you just write your servlets to expect the query parameters as normal and let the filter deal with the "pretty" URLs.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
1

Spring MVC do it very very good. But if you do not want to use third-party frameworks all you can is handle request.getRequestURI(), split this string and do what you want. For example you can use pattern /entity/action.

Georgy Gobozov
  • 13,633
  • 8
  • 72
  • 78
0

I think previous threads reply are the perfect solution. I played with web.xml file to see what I can do for and here is my results. I was able to change the web.xml to get a url similar to what you want "http://localhost:8080/do/myServlet.do". Here is web.xml file content which pertains to the servlet-name and url-pattern.

   <servlet>
      <servlet-name>do</servlet-name>
      <servlet-class>ControlServlet</servlet-class>
   </servlet>

   <servlet-mapping>
      <servlet-name>do</servlet-name>
      <url-pattern>/myServlet.do</url-pattern>
   </servlet-mapping>
</web-app>
grepit
  • 21,260
  • 6
  • 105
  • 81
  • please provide explanation if you mark it down...thanks. So, I can revise it accordingly. – grepit Jun 10 '13 at 15:52
  • You're not specifically answering the concrete question at all. You're merely stabbing in the dark and pasting an unnecessarily large block of code of which you guessed/hoped that the right lines of code would be among them. This is simply noisy and unhelpful. See also the tooltip of the downvote button. – BalusC Jun 10 '13 at 15:54
  • @BalusC , thank you for feedback. I modified my answer based on your feedback. I know the first time around that I read the question, I did not read it carefully. So, thank you for helping me to better help others :) – grepit Jun 10 '13 at 16:41
  • 1
    You're still not answering the concrete question. OP does not seem to want to use URLs with extensions either. See my answer for the expected answer. – BalusC Jun 10 '13 at 16:43