getWriter() problem

[nobr]Im not new to programming, but rather new to java web apps.

I am working on the servlet below, it was coming along great, except:

It doesn't like the getWriter() in the attLogin() function. I dont understand why considering the same getWriter appears many times throughout the application.

Thanks for any help.

import java.io.*;

import java.text.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.awt.*;

import java.awt.event.*;

import java.net.*;

import java.sql.*;

import util.HTMLFilter;

publicclass PIMextends HttpServlet{

publicvoid doGet(HttpServletRequest req,

HttpServletResponse re)

throws IOException, ServletException

{

re.setContentType("text/html");

PrintWriter out = re.getWriter();

out.println("<html>");

out.println("<body>");

out.println("<head>");

out.println("<title>Person Information Manager</title>");

out.println("</head>");

out.println("<body bgcolor=\"white\">");

out.println("<h3>Personal Information Manager</h3>");

String userName = req.getParameter("username");

String passWord = req.getParameter("password");

String homePage = req.getParameter("Home");

String appLists = req.getParameter("Lists");

String appCalender = req.getParameter("Calender");

String appFinance = req.getParameter("Finance");

out.println("<P>");

out.println(userName);

out.println(passWord);

out.println("<a href=\"?Home\">Home</a> | ");

out.println("<a href=\"?Lists\">Lists</a> | ");

out.println("<a href=\"?Calender\">Calender</a> | ");

out.println("<a href=\"?Finance\">Finance</a> <br><br> ");

if (homePage !=null){

out.println("Home:");

}

elseif (appLists !=null){

out.println("Lists:");

printDatabase ( re );

}

elseif (appCalender !=null){

out.println("Calender:");

}

elseif (appFinance !=null){

out.println("Finance:");

}

out.close();

}

publicvoid doPost(HttpServletRequest req,

HttpServletResponse re)

{

attLogin(re);

}

privatevoid printDatabase ( HttpServletResponse re )

{

try{

String url="jdbc:mysql://localhost/jbsdb";

String query ="SELECT * FROM Person";

Class.forName ("com.mysql.jdbc.Driver");

//Class.forName("org.gjt.mm.mysql.Driver");

Connection con = DriverManager.getConnection

( url,"root","" );

Statement stmt = con.createStatement ();

ResultSet rs = stmt.executeQuery (query);

printResultSet ( re, rs );

rs.close();

stmt.close();

con.close();

}// end try

catch (SQLException ex){

}// end catch SQLException

catch (java.lang.Exception ex){

}

}

privatevoid printResultSet ( HttpServletResponse resp, ResultSet rs )

throws SQLException{

try{

PrintWriter out = resp.getWriter();

out.println("<center><font color=AA0000>");

out.println("<h3>jbsJDBCServlet</h3>");

out.println("<h3>Data Retrieved:</h3>");

out.println("<table border='1'>");

int numCols = rs.getMetaData().getColumnCount ();

while ( rs.next() ){

out.println("<tr>");

for (int i=1; i<=numCols; i++){

out.print("<td>" + rs.getString(i) +"</td>" );

}// end for

out.println("</tr>");

}// end while

out.println("</table>");

out.println("</font></center>");

out.println("</body>");

out.println("</html>");

out.close();

}// end try

catch ( IOException except){

}// end catch

}// end returnHTML

privatevoid attLogin ( HttpServletResponse re )

{

PrintWriter out = re.getWriter();

out.close();

}

}

[/nobr]

[7801 byte] By [wadeeea] at [2007-11-26 18:10:40]
# 1
What's the error?Manuel Leiria
manuel.leiriaa at 2007-7-9 5:43:03 > top of Java-index,Java Essentials,New To Java...
# 2
Describe "It doesn't like it". I'm sure either the compiler complained (don't see why though) and gave a specific error message, or you had a specific runtime exception. Why truncate that to "It doesn't like it" and thus lose the valuable information it supplied?
warnerjaa at 2007-7-9 5:43:03 > top of Java-index,Java Essentials,New To Java...
# 3
Oh well, another case of post-and-bolt I see. Good luck then.
warnerjaa at 2007-7-9 5:43:03 > top of Java-index,Java Essentials,New To Java...
# 4
getWriter can cause an IOException which you don't catch or declare in a throws statement. It sais that clearly in the error message. (copied the thing into Eclipse for quick access to the exception message)
Peetzorea at 2007-7-9 5:43:03 > top of Java-index,Java Essentials,New To Java...
# 5
sorry i lost my internet for a minthanks for the helpok so i would add it to look something this?private void attLogin ( HttpServletResponse re )throws IOException{PrintWriter out = re.getWriter();out.close();}
wadeeea at 2007-7-9 5:43:03 > top of Java-index,Java Essentials,New To Java...
# 6
Nevermind my last post..I got it to work.I also need ed throws IOException on the doPost() toothanks!
wadeeea at 2007-7-9 5:43:03 > top of Java-index,Java Essentials,New To Java...
# 7

> sorry i lost my internet for a min

> thanks for the help

> ok so i would add it to look something this?

>

> private void attLogin ( HttpServletResponse re )

>throws IOException

> {

> PrintWriter out = re.getWriter();

> out.close();

>}

Yes, but then since doPost() invokes this method, it will have to either catch and deal with the possible IOException, or let it bubble up too (just like your doGet() method does).

warnerjaa at 2007-7-9 5:43:03 > top of Java-index,Java Essentials,New To Java...
# 8

Every time you eat an exception, a baby kitten dies.

catch (SQLException ex) {

// Log it? Or do something with it.

} // end catch SQLException

catch (java.lang.Exception ex) {

}

Every time you don't put close statements in in a finally block, a baby puppy is beaten to death.

mlka at 2007-7-9 5:43:03 > top of Java-index,Java Essentials,New To Java...