Forums Register Login

PrintWriter not working on servlet

+Pie Number of slices to send: Send
I've written a simple html/servlet program that has a user enter their name and password on a form and then either register or login using a submit button. I have the program working, except when a user doesn't fill in either of the text fields I can't figure out how to get it to print to the page. Right now I just have it printing to my Eclipse console which is not what I want. What am I missing? BTW, I'm very new to Java.

HTML code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Cookies</title>
</head>
<body>
<form action="http://localhost:8080/Project/Main">
<center>



User Name: <input name="name" type="text" value="">


Password:  <input name="password" type="password" value="">



<input name="register" type="Submit" value="Register">
<input name="login" type="Submit" value=" Login ">
</center>
</form>
</body>
</html>



Servlet code:

+Pie Number of slices to send: Send
Welcome to JavaRanch.

Some of your code uses "System.out.println" when it should be using "pw.println" as some of the other code does.
+Pie Number of slices to send: Send
Thank you, yes, I used System.out because I couldn't get the PrintWriter to work in that section. I just wanted to see if it was communicating the message. When I replace System.out with pw.println I get the error message: pw cannot be resolved. I tried creating a local variable pw, but that didn't work either. I can't figure out where to put the PrintWriter in this method.
+Pie Number of slices to send: Send
In the other methods you can do "PrintWriter pw = response.getWriter()", because "response" is a parameter to those methods. If you want to do the same in the "check" method, you'll have to pass "response" as a parameter to that method as well.
+Pie Number of slices to send: Send
So I added PrintWriter to the check method in a try/catch block like in the other methods and still no luck. I know I'm just putting it in the wrong place but the more I move it around the more I mess things up.

+Pie Number of slices to send: Send
It's in the right place, but you're not currently passing the "response" object as a parameter to the "check" method, so you can't use it there.

You need to pay close attention to the error messages you're getting. The one you currently have should mention "response" being recognized, not "pw" not being recognized.
+Pie Number of slices to send: Send
Thank you, that explains it and I was able to correct my errors. My new code all fixed up: Thank you again!

This tiny ad will self destruct in five seconds.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6083 times.
Similar Threads
SESSION TRACKING.
Testing Cookies
Using a method to retrieve a cookie
HTML display
JDBC/servlet connection
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:33:39.