wood burning stoves*
The moose likes Servlets and the fly likes Using servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Using servlets" Watch "Using servlets" New topic
Author

Using servlets

Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
My error from my previous post in JSP's now leads onto servlets. I was trying to figure out why i was being given the message that my data has been saved to my database, even though it hadnt. So i done some messing about with the code and found out somthing i just wish to confirm.
I have a register and login servlet, which obviously registers and logs in a user. If the user successfully logs in, then the servlet points them to here


Now in this jsp, the user has four buttons to choose, each one taking them to a new jsp. I did not however provide a servlet to this jsp in order to point the user to the new jsp. I thought i could get away with doing this


This brings up the jsp no problems, but it seems to make it no longer part of my hierachy. I am pretty sure this is my problem as i removed all the register and logn servlet and code, and i pointed my browser straight to the jsp where the user can enter a competitor (The one where no data was previously going to database). But this time it works and data is being saved. Does this make sense? So when i create a project, all linking should be done through servlets? Or can anyone think of another reason why this might of been happening?
cheers
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Putting an onclick on a submit button is a recipe for disaster. What's your real intention?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I didn't completely understand the question but if you want four submit buttons to point to four different JSPs, then you can use separate forms for them like this



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
Its difficult to explain my intention, but basically i want that jsp to load up different jsp's when a button is click. The problem i have at the moment, using the onClick command, everything loads up ok but the data never goes to the database. Its like i need a servlet to handle the actions. So if i use


Possibly removing the first line, i could then prov ide a servlet like


This way i am handling the forwarding through a servlet, and involving my Bean with it. Would this be a better approach?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

If you are not submitting a form, don't use a submit button. If you are submitting a form, use the form action to specify the target, not an onclick.

In any case, forms should always be submitted to servlets, not JSPs.
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
I have tried a lot of different things, but now i am a bit stuck as to what to do. SO my user logs in, and my UserServlet handles this


So if the result is one, it brings up my CreateAtlDatabase.jsp, which is the first display page when they log in. This page has three buttons. One of them is too add a comeptitor. So i have a servlet which handles adding a competitor, which request the competitors data from the addCompetitor.jsp and sends it off to the database.
This is my problem. If i use onClick in my CreateAthDatabase.jsp, all the pages work, and everything does what it is suppossed to do, Even getting a success message back saying competitor has been entered, but nothing gets saved into the database.
If i point my browser straight to AddCompetitor.jsp, rather than the starting point off my application, everything works fine as before but the data actually gets saved to the database this time(this would tend to tell me that the problem lies in CreateAthDatabase).
If i create a new servlet for CreateAthDatabase, when i click on the Add competitor link, which should take me to the AddCompetitor.jsp, i get back the error
The requested resource (/cmt3082/servlet/servlet/DatabaseServlet) is not available.
Which looks like its searching through two Servlets. SO this is where i am confused. What can i do with this CreateAthDatabase.jsp so that it forwards to the correct page (and allows the data to be saved)
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Nick Price wrote:The requested resource (/cmt3082/servlet/servlet/DatabaseServlet) is not available.
The relative URL which you specified was wrong. This may occur when you're currently in for example "/cmt3082/servlet/SomeServlet" and dispatched the request to "servlet/DatabaseServlet" instead of "/servlet/DatabaseServlet" or "DatabaseServlet". The context root of the requestdispatcher is "/cmt3082". Also see the API docs: http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)
 
Don't get me started about those stupid light bulbs.
 
subject: Using servlets
 
Similar Threads
Chapter 2 (HFSJ) notes , may be useful for anyone
ServletContextListener???
help converting asp appliction to servlets
When to use a JSP page?
is it possible