How is the HTML form action created from servlet-mapping url-pattern?
Joined: Sep 07, 2004
I am reading HF�s SCWCD-book Servlets & JSP and is currently studying their beer selection web application created as a struts. And I have understood everything from their good explanations and got everything to work after some playing with their examples. But there is one small thing I can�t figure out. How is the HTML FORM action attribute constructed, and particularly how is it affected from what I enter within the <url-pattern> whithin the <servlet-maping> element?
In the book do they have the <url-pattern>*.do</url-pattern> to let the ActionServlet handle all requests which maybe is the thing to do in the real life. But I wan�t to play to understand so I changed it to <url-pattern>/ch14/SelectBeer.do</url-pattern> which I think should have the ActionServlet to only handle requests to just that specific servlet. Maybe this is stupid : ) but stupid experiments support learning. When I use *.do does everything work perfectly but when I change to my path do the HTML FORM action attribute on the form.jsp page change to the name (root) of my Webapplication that I have called SCWCD and gets deployed as SCWCD.war.
So how do I use the <url-pattern>-element in the web.xml DD for ActionServlet when I am creating a struts?
I have created/deployed the example this way ...
The HTML form whithin the form.jsp page:
And the web.xml DD:
I have put form.jsp and result.jsp in the "WebRoot\ch14" folder and BeerSelectForm.java and BeerSelectAction.java in the pacakage ch14.struts.web.
Effective Java Programming Language Guide: Think in terms of exported APIs because it tends to improve the quality of the software you write
Joined: Sep 07, 2004
Gosh! I forgot to show what my HTML FORM action look like in the generated form.jsp when I use my own <url-pattern>. This is how it looks like and that is defenitley NOT a valid path:
When i t works as it should, using the <url-pattern> *.do does it look this:
Beng Wooi Toh
Joined: Dec 02, 2005
If you wish to use the Struts Framework, you need to have these in your web.xml :
This means any request that ends with the pattern xxx.do (which matches the regex *.do) will be directed to the Struts ActionServlet.
Good web programming architecture dictates that we �guard� our jsps. See pg 88 of �Core J2EEPatterns: Best Practices and Design Strategies, Second Edition� (ISBN: 0131422464) So, we usually do not let browsers access the jsps directly. This is one of the purposes of using Struts � to let a servlet (ActionServlet) intercept the request call, so that you can decide in your action classes 1) whether to serve the jsp to the browser or 2) which is the correct jsp to serve to the browser... among other decisions that you can make.
Struts recommends us to always direct requests to the action servlet... so almost all of your requests/URLs in your jsp should point to something.do.
Leave <url-pattern>*.do</url-pattern> as it is. Do not hardcode it to a specific URL (unless you really know what you want and what you are doing). Real world applications leave that as it is.