aspose file tools*
The moose likes Servlets and the fly likes Can you invoke a servlet on the context path page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can you invoke a servlet on the context path page?" Watch "Can you invoke a servlet on the context path page?" New topic
Author

Can you invoke a servlet on the context path page?

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 162

Hi,

is it possible to call a servlet when you launch your web project? To explain it better my home page pops up when I go to the url http://localhost:8080/MyProject/ I want to use entity beans to get information from a database so I can display everything from a particular table on the home page. If I am to use a servlet though to assign the data in the tables to an attribute how do I do this for the home page? At the moment I have a String userPath variable set to request.getContextPath. But doesn't the context path method return whatever comes after it i.e. /MyProject/...... where what you request would be on the dots? How can I call the servlet straight away by just requesting the home page?

Thank in advance!

Alan
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

If you have a index.jsp (or something similar configured as a welcome file) then the server will serve it when partial URL is requested as in this case. But you can move your home page inside WEB-INF and make a redirect to the proper URL to there which is mapped to a servlet where you do your logic...


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 162

Hi,

I think I have something working. When I use firebug to check the content of the div (called leftColumn) the entity ids are being mapped to the jsp for each loop i created. I still can't get the names in the table to show up though. The names are just a list of counties in the table. I put the county names in the servlet context to be available to all request types. Heres my servlet (/Ireland is my project name/path) :



and in the jsp index page I have:


Theres more html but the bit im focusing on is trying to extract the county names by looping through the findAll result attached assigned to the attribute. Nothing is showing up though. What am I missing?

Thanks again,
Alan
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 162

I figured it out thanks!
Daniel Val
Ranch Hand

Joined: Jan 09, 2012
Posts: 44
Alan Smith wrote:Hi,

is it possible to call a servlet when you launch your web project? To explain it better my home page pops up when I go to the url http://localhost:8080/MyProject/ I want to use entity beans to get information from a database so I can display everything from a particular table on the home page. If I am to use a servlet though to assign the data in the tables to an attribute how do I do this for the home page? At the moment I have a String userPath variable set to request.getContextPath. But doesn't the context path method return whatever comes after it i.e. /MyProject/...... where what you request would be on the dots? How can I call the servlet straight away by just requesting the home page?

Thank in advance!

Alan


- Create the servlet
- Assign some url patern like home.servlet (something like that)
- Try to see if you can attach a welcome page in web.xml that would look like that (home.servlet)

I have to admit that I never tried that, but at JSF you usually put index.jsf and it knows where to go (this is also a servlet address).

Other approach:

- You put the welcome page to index.jsp (that works for sure)
- Inside the jsp, the ONLY stuff is

<jsp:forward page="home.servlet" />

That works as well.

D.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can you invoke a servlet on the context path page?