• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PrintWriter not working on servlet

 
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Betty Christiansen
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, that explains it and I was able to correct my errors. My new code all fixed up: Thank you again!

 
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic