This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes bookstore application error.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "bookstore application error.." Watch "bookstore application error.." New topic
Author

bookstore application error..

mallika shah
Ranch Hand

Joined: May 11, 2006
Posts: 164
Hello firends,
I have some problem running this code..-->
web.xml
<web-app>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.loginServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>

portion of register page
<form name="register" action="loginServlet" method="POST">
<table>
<h3>Enter Your Details</h3>
<tr>
<td>First Name : </td>
<td><input type="text" name="firstName"></td>
<input type="submit" value="Register" name="register">

loginServlet.java
public void doPost (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
// then write the data of the response
//out.println("<body bgcolor=\"#ffffff\">" + "<center>" + "<hr> <br>  " + "<h1>" + "<font size=\"+3\" color=\"black\">Jayana's Bookstore</font>" + "</h1>" +
//"</center>" + "<br>   <hr> <br> ");
String firstName=request.getParameter("firstName");
String lastName=request.getParameter("lastName");
request.setAttribute("fName",firstName);
request.setAttribute("lName",lastName);
try {
BookDB bookDb=new BookDB();
int i=0;
i=bookDb.getNumberOfBooks();
System.out.println("There are total "+ i + " Books available");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

and lastly BookDB.java, which accesses he database

public BookDB() throws Exception{

}

private static Connection getConnection(String strURL,String strUsername, String strPassword){
try {
return DriverManager.getConnection(strURL, strUsername, strPassword);
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

public void removeConn () {
try {
con.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}


public int getNumberOfBooks() {
//public static void main(String args[]){
int count=0;
books=new ArrayList();
try{
String strSQL = "Select * from bookstore.books ";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connMySQL = getConnection("jdbc dbc:mySQLConn", "root", "mallika");
try {
ResultSet rs = connMySQL.createStatement().executeQuery(strSQL);
while(rs.next()){
System.out.println("Book Title : "+rs.getString(4));
count++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block

e1.printStackTrace();
}
}catch(Exception e2){
System.out.println(e2.getMessage());
}
return count;


My problem is- the actions of register page does take me to loginServlet, as i see thishttp://localhost:8080/bookstore1/loginServlet in my browser but it comes to be a blank page..

the BookDB does run and returns 4 books if i run it putting a main() method in it.Why is not my servlet working to access the BookDB and show the result??

Thanks,
Mallika
Anupama Ponnapalli
Ranch Hand

Joined: Jun 12, 2006
Posts: 66
Did you try uncommenting this line:

//out.println("<body bgcolor=\"#ffffff\">" + "<center>" + "<hr> <br> " + "<h1>" + "<font size=\"+3\" color=\"black\">Jayana's Bookstore</font>" + "</h1>" +
// "</center>" + "<br> <hr> <br> ");
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

I'm surprised the compiler didn't mind that you have a Java class (loginServlet.java) that doesn't start with a capital letter.
[ March 12, 2007: Message edited by: Marc Peabody ]

A good workman is known by his tools.
mallika shah
Ranch Hand

Joined: May 11, 2006
Posts: 164
Thanks friends for the reply to my question..

Mark, there shouldnt be any problem if you define a class name with starting letter as small..
its just a good practice to write LoginServlet insted of loginServlet..but not a problem to a compiler..

also wanted to tell that i discoved teh problem,It was because , i had got an object out of Printwriter and i was using System.out.println("") instead of just out.println("");
and this small thing was creating a whole problem , nothing was getting printed to the screen because of this..
But when i took out the System and used out.println everything was fine..

thanks,
mallika
mallika shah
Ranch Hand

Joined: May 11, 2006
Posts: 164
Hello Friends,
here is a question from JPilot exam,

Which of the following are valid uses of the <jsp:useBean> action?
A.
<jsp:useBean id="myBean" class="MyBean" scope="session">
B.
<jsp:useBean id="myBean" class="MyBean"/>
C.
<jsp:useBean id="myBean" class="MyBean" beanName="SomeOtherBean"/>
D.
<jsp:useBean id="myBean" type="MyBean">

My answers were A and B, but it says B and D..
why so?
what is wrong with A??if you say A is not ending with a forward slash"/" but its the same with D. and why is D selected as correct ans?

thanks,
mallika
 
Don't get me started about those stupid light bulbs.
 
subject: bookstore application error..