Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet-mapping to ROOT

 
Richard Berglund
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm a bit new to Java servlets, so I apologize for the, what I'm sure is a, dumb question, but it's been very frustrating for me.

I'm toying around with building a basic servlet. I want the servlet to handle any requests on the root. Initially, my web.xml had a servlet mapping of,

<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet/*</url-pattern>
</servlet-mapping>

so my URL had to be http://localhost:8080/ProjectName/MyServlet/, but I wanted it to be http://localhost:8080/ProjectName/.

I updated my web.xml to be the following,

<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Which seemed logical to me

Strangely, all my web resources are now inaccessible through a relative path. For example,

<img src="images/main.png" /> used to work just fine with the original web.xml, but with the modified version it no longer works.

So basically, I'm looking for some understanding on:

1. Why does it work like this?
2. What SHOULD I have changed to access the root level and still use relative paths?
3. Are there any other best practices when using servlets?

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<url-pattern>/*</url-pattern>


Because * matches everything, this maps everything to your servlet. Bad idea. Very bad.

What you really want to do is to map your servlet as the "welcome file".

Look up <welcome-file-list> in the Servlets Spec.
 
Richard Berglund
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
<url-pattern>/*</url-pattern>


Because * matches everything, this maps everything to your servlet. Bad idea. Very bad.

What you really want to do is to map your servlet as the "welcome file".

Look up <welcome-file-list> in the Servlets Spec.


Thanks for the quick reply!

Yes, I was concerned that /* could be a bad idea, appears it is

I played around earlier with the <welcome-file-list>, but it didn't seem to do anything (I had an index.jsp page that would display in the event the servlet didn't show anything). I'll play around with it some more.

I do have a follow up question, once I get the servlet to handle the welcome-file, what is the typical process of handling subsequent links, since that would have to also be handled by the servlet. Most of my background is in php development, which is nothing more than a subsequent page, but I'm starting to notice significant differences between php/jsp and servlets.

Thanks!



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to create a front controller? If not, each link should have its own servlet.

You might find this article helpful to understand proper Java web app structure.
 
Richard Berglund
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Are you trying to create a front controller? If not, each link should have its own servlet.

You might find this article helpful to understand proper Java web app structure.


Yes, my plan was to use it as a front end controller. Not certain whether or not that's a great idea, just following the information I've been reading.

Overall, I'm just trying to get acquainted with Java web development. Unfortunately, much of the information I've read doesn't offer much on overall design (when to use server-side vs client-side scripting, etc). Years ago I used to develop in php, and with that all database, file, etc processing was handled in server scripts and dynamic content was handled in javascript. In some examples I've found I noticed the use of static html pages using sevlets or services to process actions (form submit, etc). But that makes me wonder how to handle a session without writing that into each service.

Thanks for the link, I'll read that now!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Berglund wrote:Yes, my plan was to use it as a front end controller. Not certain whether or not that's a great idea, just following the information I've been reading.


The Front Controller is a good pattern for web apps. But you might want to make sure that you've got straight-forward servlets working and pretty well understood before tackling it.

Overall, I'm just trying to get acquainted with Java web development. Unfortunately, much of the information I've read doesn't offer much on overall design (when to use server-side vs client-side scripting, etc).

There's no magic bullet, of course. But generally all I do everything to set up the page on the server (including initial dynamic conent), and handle client-side interactions in JavaScript with jQuery.

In some examples I've found I noticed the use of static html pages using sevlets or services to process actions (form submit, etc)

Not exactly sure what you mean by that, but the article I linked to pretty well lays out responsibilities.

But that makes me wonder how to handle a session without writing that into each service.

The session is handled for you by the servlet container. You don't need to do anything overt to maintain it.

Thanks for the link, I'll read that now!

Enjoy!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, when you get to JSP, be sure you focus on modern JSP. That means no Java code in the page. None.

Be sure that you focus on the JSTL and EL and not Java scriptlets.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic