NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Servlet Database Access

 
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users
View previous topic :: View next topic  
Author Message
maxpeter



Joined: 05 Feb 2013
Posts: 9
Location: Kenya

PostPosted: Mon Feb 18, 2013 6:06 am    Post subject: Servlet Database Access Reply with quote

am trying to program a servlet which will access a database in the background. am using tomcat 7 as the server and an embedded Apache Derby database. it is a tutorial on the advanced java book using the animalsurvey database. the app is such that whenever a client clicks on a radio button, the choice is sent to a database which interprets it as a vote for a given animal. whenever i run the program, the index.xhtml file loads Ok, but whenever i click on an animal, i get the errors shown below saying the server says the database cannot be found and that the "Document base" does not exist or cannot be read. what could i be doing wrong? the database is located in the default windows user directory "C:/Users/Me/animalsurvey".

.....................................................
the index.xhtml file:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Survey</title>
</h:head>
<h:body>
<form method="post" action="SurveyServlet">
<p>What is your favourite pet?</p>
<p>
<input type="radio" name="animal" value="1" />Dog
<br />
<input type="radio" name="animal" value="2" />Cat
<br />
<input type="radio" name="animal" value="3" />Bird
<br />
<input type="radio" name="animal" value="4" />Snake
<br />
<input type="radio" name="animal" value="5" checked="checked" />None
<br />
</p>
<p>
<input type="submit" value="Submit" />
</p>
</form>
</h:body>
</html>

.........................................................
the SurveyServlet.java file:

package com.maxp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.text.DecimalFormat;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SurveyServlet extends HttpServlet
{
private Connection connect = null;
private PreparedStatement updateVotes = null,
totalVotes = null,
resultSet = null;
private String xmlVersion = "<?xml version = \"1.0\"?>";
private String xmlHeader = "<!DOCTYPE html PUBLIC \"-//w3c//DTD"
+ "XHTML 1.0 Strict//EN\" \"http://www.w3.org"
+ "/TR/xhtml1/DTD/xhtml1=strict.dtd\">";
private String htmlHeader = "<html xmlns=\"http://www.w3.org/1999/xhtml\">";

public void init(ServletConfig config) throws ServletException
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
connect = DriverManager.getConnection("jdbc:derby:animalsurvey");

updateVotes = connect.prepareStatement("update surveyresults"
+ " set votes = votes + 1"
+ " where id = ?");

totalVotes = connect.prepareStatement("select sum(votes) from surveyresults");

resultSet = connect.prepareStatement("select surveyoption, votes, id"
+ " from surveyresults"
+ " order by id");
}
catch(Exception e)
{
e.printStackTrace();
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");

PrintWriter out = response.getWriter();

DecimalFormat two = new DecimalFormat("0.00");

out.println(xmlVersion);
out.println(xmlHeader);
out.println(htmlHeader);
out.println("<head>");

int value = Integer.parseInt(request.getParameter("animal"));

try
{
updateVotes.setInt(1, value);
updateVotes.executeUpdate();

ResultSet totals =totalVotes.executeQuery();

totals.next();
int total = totals.getInt(1);

ResultSet results = resultSet.executeQuery();

out.println("<title>Thank you!</title>");
out.println("</head>");

out.println("<body>");
out.println("<p>Thank you for participating.");
out.println("<br />Results:</p><pre>");

int votes;

while(results.next())
{
out.print(results.getString(1));
out.print(": ");
votes = results.getInt(2);
out.print(two.format((double) votes / total * 100));
out.println("% responses: ");
out.println(votes);
}
results.close();

out.println("Total responses: ");
out.println(total);

out.println("</pre></body></html>");
out.close();
}
catch(SQLException se)
{
se.printStackTrace();
out.println("<title>Error</title>");
out.println("</head>");
out.println("<body><p>Database Error Occured. ");
out.println("Try again later.</p></body></html>");
}
}

public void destroy()
{
try
{
updateVotes.close();
totalVotes.close();
resultSet.close();
connect.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
}
}

...........................................................
the tomcat server error report:

Using CATALINA_BASE: "C:\Users\MaxP\AppData\Roaming\NetBeans\7.2.1\apache-tomcat-7.0.27.0_base"
Using CATALINA_HOME: "C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.27"
Using CATALINA_TMPDIR: "C:\Users\MaxP\AppData\Roaming\NetBeans\7.2.1\apache-tomcat-7.0.27.0_base\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk1.7.0_11"
Using CLASSPATH: "C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.27\bin\bootstrap.jar;C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.27\bin\tomcat-juli.jar"
Feb 18, 2013 8:54:35 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.7.0_11\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.7.0_03\bin;C:\Program Files\Java\jre7\bin;C:\Program Files\Java\jdk1.7.0_03\db\bin;C:\Program Files\Java\jdk1.7.0_03\db\lib;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\masm32\include;C:\masm32\bin;C:\masm32\lib;C:\masm32;C:\stdlib\INCLUDE;C:\stdlib\LIB;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Program Files\Java\jdk1.7.0_10;C:\Program Files\Java\jdk1.7.0_10\db\bin;C:\glassfish3\bin;C:\glassfish3\glassfish\bin;C:\glassfish3\javadb\bin;C:\glassfish3\jdk7\bin;C:\glassfish3;C:\glassfish3\glassfish\lib;C:\glassfish3\glassfish\modules;C:\glassfish3\glassfish\modules\autostart;C:\glassfish3\install\bin;C:\glassfish3\install\lib;C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.27\bin;C:\masm32\include;C:\masm32\bin;C:\masm32\lib;C:\masm32;.
...
SEVERE: Error starting static Resources
java.lang.IllegalArgumentException: Document base D:\Documents\programs - java\Web Apps\JavaServletsTutorial\build\web does not exist or is not a readable directory
at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:140)

...

Feb 18, 2013 8:54:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error in resourceStart()
Feb 18, 2013 8:54:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error getConfigured
Feb 18, 2013 8:54:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/JavaServletsTutorial] startup failed due to previous errors
Feb 18, 2013 8:54:38 AM org.apache.catalina.startup.HostConfig deployDescriptor

...

SEVERE: Error starting static Resources
java.lang.IllegalArgumentException: Document base D:\Documents\programs - java\Web Apps\Java Servlet Tutorial\build\web does not exist or is not a readable directory
at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:140)

...

SEVERE: Error in resourceStart()
Feb 18, 2013 8:54:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error getConfigured
Feb 18, 2013 8:54:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/Java_Servlet_Tutorial] startup failed due to previous errors
Feb 18, 2013 8:54:38 AM org.apache.catalina.startup.HostConfig deployDescriptor

...

SEVERE: Error starting static Resources
java.lang.IllegalArgumentException: Document base D:\Documents\programs - java\Web Apps\Using JDBC from Servlet\build\web does not exist or is not a readable directory
at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:140)

...

Feb 18, 2013 8:54:38 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error in resourceStart()
Feb 18, 2013 8:54:39 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error getConfigured
Feb 18, 2013 8:54:39 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/JDBC_Servlet] startup failed due to previous errors
Feb 18, 2013 8:54:39 AM org.apache.catalina.startup.HostConfig deployDescriptor

...

SEVERE: Error starting static Resources
java.lang.IllegalArgumentException: Document base D:\Documents\programs - java\Web Apps\Servlet Database Acess\build\web does not exist or is not a readable directory
at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:140)
...

SEVERE: Error in resourceStart()
Feb 18, 2013 8:54:57 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error getConfigured
Feb 18, 2013 8:54:57 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/Servlet_Database_Acess] startup failed due to previous errors

...

INFO: Server startup in 40416 ms
java.sql.SQLException: Database 'animalsurvey' not found.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
...
...............................................
the web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>SurveyServlet</servlet-name>
<servlet-class>com.maxp.SurveyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SurveyServlet</servlet-name>
<url-pattern>/SurveyServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
.................................................................

what could be the problem? i want to understand in details what went wrong in the program.
Back to top
jyeary



Joined: 21 Oct 2008
Posts: 639
Location: Simpsonville, SC

PostPosted: Mon Feb 18, 2013 5:39 pm    Post subject: Servlet Database Access Reply with quote

You are mixing JSF and HTML forms and pages. I am going to guess this is not what you intended. Create a plain HTML page and add your form.

John

____________________________


John Yeary
____________________________
Back to top
maxpeter



Joined: 05 Feb 2013
Posts: 9
Location: Kenya

PostPosted: Tue Feb 19, 2013 6:06 am    Post subject: Servlet Database Access Reply with quote

no, my problem is on the database end. i know about not mixing the html and jsf part, but i will deal with that later. for now i want to know why my database is not accessible by the server
Back to top
jyeary



Joined: 21 Oct 2008
Posts: 639
Location: Simpsonville, SC

PostPosted: Tue Feb 19, 2013 7:03 pm    Post subject: Servlet Database Access Reply with quote

There are a number of serious issues around the directories where your application is located. For example,
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java EE Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo