Michael,
I'm sure everyone else is gonna tell you this too, but here are some
pointers from just glancing over the code.
- set up your db connection in the init() method, not doGet(). doGet()
is executed every time the servlet is called with an HTTP GET. So,
effectively, you are loading the driver and creating a connection and
DBThing object and blah blah blah every time your servlet is called. That's
a performance killer.
- better yet, use a Connection Pool that's created on startup. Write
your own, or there are many available. I'm sure others will provide links...
- as far as your question: I'm assuming you're using an HTML form to
POST/GET to the servlet. There are alot of ways you can go, but a simple one
is to have a parameter called 'databaseaction' or whatever, and that
indicates which operation the servlet will perform from it's doGet() or
doPost(). Alternatvely, you can have a different HTML form/page for each
action that calls
the servlet (or a different servlet, although that's pretty inefficient)
with a different parameter.
>
> This is easy enough, but how do I have the servlet update its own output
> in the web browser so that it reflects the new changes? Is there a
> refresh command or something.
You can have the servlet (or JSP) just call itself, and at the end of
processing, just display whatever you were going to display normally.
Servlets are based on a request/response model, so once the servlet has
finished returning data to your browser, it is not going to attempt to push
new data out. It would have to be called again after the update takes place.
> I was thinking having a jsp page where the user inputs the changes, and
> then POST it to the servlet.. still not sure how the page gets updated.
> Anybody have any example sites/code??
You can have the form on the JSP just call the JSP again. The JSP tests for
the right combination of parameters and performs the appropriate actions...
e.g. in 'faq.jsp' (this is basically just snippits)
<html>
<body>
<%
JDBCConnectionPool connectionPool =
(JDBCConnectionPool)application.getAttribute("edu.tcnj.cs.cmsc446.Connection
pool");
%>
<form name="myform" action="faq.jsp" method="POST">
...
</form>
<%
/* get params, test for form submission, take appropriate action */
String submitted=request.getParameter( "Submit" );
String questiontext=request.getParameter( "questiontext" );
String username=request.getParameter( "username" );
if( submitted!=null
&& submitted.equals( "Submit Question" )
&& questiontext!=null
&& !questiontext.equals( "" )
&& username!=null
&& !username.equals( "" ) ){
edu.tcnj.cs.cmsc446.SQLWorker sqlWorker = new
edu.tcnj.cs.cmsc446.SQLWorker();
try{
sqlWorker.setConnection( connectionPool.getConnection() );
sqlWorker.submitQuestion( questiontext, username );
sqlWorker.logQuestionPost( username, "question",
request.getRemoteAddr() );
}catch(Exception badQues){
application.log( "faq.jsp: error submitting new question="+badQues );
}finally{
if( connectionPool!=null )
connectionPool.free( sqlWorker.getConnection() );
}
}else{
out.println( "<p>No question asked at this time. Enjoy browsing.</p>" );
}
%>
<!-- more html -->
<%
edu.tcnj.cs.cmsc446.SQLWorker sqlWorker = new
edu.tcnj.cs.cmsc446.SQLWorker();
try{
sqlWorker.setConnection( connectionPool.getConnection() );
Vector entries = sqlWorker.getQuestions();
if( entries!=null ){
Question ques=null;
for( int i=0;i<entries.size();i++ ){
ques = (Question)entries.elementAt( i );
out.println( "<tr><td width=\"23%\"><font face=\"Arial, Helvetica,
sans-serif\" size=\"2\"><a
href=\"answer.jsp?refNum="+ques.getRefNum()+"\">"+ques.getRefNum()+"</a></fo
nt></td>" );
out.println( "<td width=\"77%\"><font face=\"Arial, Helvetica,
sans-serif\" size=\"2\">"+ques.getQuestion()+"</font></td></tr>" );
}
}else{
out.println( "<tr><td>no entries</td><td>no questions at this
time</td></tr>" );
}
}catch( Exception e ){
application.log( "faq.jsp: error getting all questions="+e );
}finally{
if( connectionPool!=null )
connectionPool.free( sqlWorker.getConnection() );
}
%>
</body>
</html>
Regards,
Michael
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>