Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Making attributes from textboxes available to other servlets and storing the latest passed variable.  RSS feed

 
Rian Mhor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make the variables that will be taken in from a type='text' box in HTML in NetBeans available to the other servlets as well as store and display the latest value passed into these boxes on the servlet page when the submit button is clicked or display null if the box has not yet received a value. However, I do not understand how to do this and do not understand as to why my current output is always null?? Can someone guide me as to how to fix this? My main questions , really, are;

i) Is ServletContext().getAttribute("name of my textbox") really the way to pass the attributes to all servlets?
ii) Is ServletContext() passed in the service request?
iii) How to store the latest value passed into the submit box?

Here is my code
index.html




FirstServlet




SecondServlet


This is incredibly confusing stuff.....
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet context is indeed a scope that makes its properties available to all servlets and JSPs in the web app. All. So make sure that's what you want. If the data is specific to a single user, rather than all users, then the session scope is the one to use.

Your notation (ServletContext().getAttribute()) is not correct. In a servlet, you'd obtain a reference to the servlet context (also known as "application context") from the request and then call its setAttribute() and getAttribute() methods.

In a JSP, you'd just reference the property by name in an EL expression. For example, if the property were named "fred", you'd obtain its value with ${fred}

P.S. Why are you emitting HTML code in a servlet rather than using a JSP?
 
Rian Mhor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Your notation (ServletContext().getAttribute()) is not correct. In a servlet, you'd obtain a reference to the servlet context (also known as "application context") from the request and then call its setAttribute() and getAttribute() methods.


Bear, I do not understand. I am now using that code but it still won't work, it only displays null. Why can I not get my information from the HTML code? Can someone just correct my code and then explain to me where I am going wrong? I learn best through this method, I am so sorry but I cannot learn unless shown. Really sorry, it just how I am.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rian Mhor wrote:Why can I not get my information from the HTML code?


You are confusing scoped properties (poorly named "attributes") with request parameters. When a form is submitted to the servlet, the parameters are available via the getParameter() family of methods on the request.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Your form input elements need to be inside the form to be submitted when the form is submitted.
 
Rian Mhor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following code and then tried printing out the string variables but it still won't work. I don't understand where I am going wrong?? I got the parameters but the values aren't posting. It's still saying null. Is this not what I am supposed to do?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you see my post just before yours? I may have posted it while you were typing that last one.

And remove the casts to Strings. They're superfluous. All request parameters are always strings.
 
Rian Mhor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't get it. I did everything but it's still not working. Am I missing something major??

Index.html




FirstServlet


SecondServlet


I've done everything you've asked but it still won't work. Is there something wrong with my laptop?? Or is my code still wrong. It won't display what I enter.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you verified that the servlet is actually being called? The form action is suspect.

Also move your variables into the method declaration. Member variables in a servlet are a big no-no. It's certainly not the cause of your current difficulties but will create a world of hurt later on.
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here you have six (6) forms:
1st form: line 6-7, has only one parameter: firstName => no way to submit it, unless you use JavaScript, which you're not
2nd form: line 9-10 has only one parameter: surname => no way to submit it, ditto
3rd form: line 12-13 has only a submit button on it, no parameters => you can submit it but FirstServlet will not get anything
4th form: line 15-16, has only one parameter: integer => no way to submit it, ...
yada, yada, yada

Do you see what you're doing wrong? As Bear said, your input fields and your submit button need to be inside the one <form></form> element to be submitted together. It doesn't work the way you seem to think it works.
 
Rian Mhor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand. They ARE in the <form></form> tags......I don't understand why you're saying they're not when they are. I still don't understand what you're asking me to change. Can someone show me EXACTLY where I am going wrong....? Please?
 
Junilu Lacar
Sheriff
Posts: 10879
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need only ONE <form></form> with all the input and submit things inside it. You currently have 6 separate forms, each with their own input thing inside it.
You need it this way:

Just the one form per servlet you want to submit to.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need just one form.

[Edit: Junilu beat me to it!]
 
Rian Mhor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am calling it correctly.
 
Rian Mhor
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<html>
<head>
<title>Lab One</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body> <form action="FirstServlet" method="POST">
First Name: <input type="text" name="firstName">


Surname: <input type="text" name="surname">


<input type ="submit" name="Submit">


Integer: <input type="text" name="integer">


Float: <input type="text" name="float">


<input type ="submit" name="Submit2"> </form>

</body>
</html>


This is my index.html form now but it won't compile? It says "The module has not been deployed.
See the server log for details." Using netbeans
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problems in HTML cannot cause that error. Is there any other information shown in the logs where you saw that message?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!