Advise on how to transfer scriptlet to Servlet

I have a pagination working great in a Scriptlet on Tomcat container.

I cant download any Java software at my workplace

(due to restrictions) to get JSTL or pagination programs etc

so I had to manually make my Scriptlet pagination results page.

Here for example is how it would look if user was on Page 2 of a 4 page results Pagination page:

Previous1 234Next

Now would like advise on how to put it in a Servlet instead of Scriptlet. I assume it should go in Servlet (Controller part) and not bean?

The form searches 1 field value and it goes to the action page (with all Scriptlets) below.

The below excerpts taken from my action page where it uses several out.println statements to output the html pagination parts.

Any advise on how to convert this action page to Servlet or Bean?

Should I just put this whole thing in a Servlet and the database connections can go into a seperate Servlet?

//pagination code

<%

//alot of declaration variables here

String url ="jdbc:oracle:thin:@SERVERNAME:1521:DBNAME";

try{

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection conn = DriverManager.getConnection(url,"scott","tiger");

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("select * from person");

..

while(rs.next())

{

resultSetRecordTotal++

}

out.println("Displaying " + BeginVar +" records out of " + anotherVar +" from total database records equal to " + resultSetRecordTotal +" records.<br>");

for(int i = 1;i <= pagesComputationTotal;i++)

{

...

//this part outputs the pagination page links

out.println("<a href=mypage.jsp?beginVar=" + varOne +"&nextBeginVar=" + i +">" + i +"</a>");

....

}

if(somevalue ==true)

{

//this part shows the Next Link

out.println("<a href=mypage.jsp?anotherVar=" + anotherVar +"&nextBeginVar=" + (aVar + 1) +">Next</a>");

}

....

//close statements for first db connection

try{

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection conn2 = DriverManager.getConnection(url,"scott","tiger");

Statement stmt2 = conn2.createStatement();

ResultSet rs2 = stmt.executeQuery("select * from person");

while(rs2.next())

{

String myInfo = request.getParameter("theName");

//another condition here that gives me DB records for pagination getting maxrows and begining row values

out.println("<br>" + myInfo);

}

//close statements for second db connection

%>

I would appreciate any Servlet and/or bean advise on how this should be done.

[4118 byte] By [kensingtona] at [2007-11-26 21:39:57]
# 1

Moving JSP scriptlet code to a Servlet is even more complicate and highly discouraged.

If you can't download stuff from the Internet, then you could carry a USB flash key to work, or a floppy disk or burn a CD - there are so many other options.

Plus the advantage of JSTL is that you don't need any Administrative rights to install it. Because JSTL 1.1 is just a set of JAR files, configure web.xml , use the right web container that supports Servlet 2.4 spec.

Why are you unnecessarily complicating your life over Servlets?

Use

JSP/ JSTL only for front end display

Servlet as controller and one of the ways to pass a JavaBean (data) between JSPs and the middle application layer.

Use JavaBeans and Java classes for the middle layer.

Use JDBC or Hibernate for the data access layer.

appy77a at 2007-7-10 3:24:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
I cant use anything like floppy or CD or put any info on our system to many restrictions so JSTL is not an option.In my case I want to put into a Servlet or Bean so I can gain experience with it even if it is tedious with my Scriptlet.
kensingtona at 2007-7-10 3:24:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

If you really must use servlets, here's a simple servlet you can write the rest of the code in it, you can create private methods within the servlet. Basically servlets are just like any other Java classes , but they have 2 special methods doGet and doPost and some other methods.

package somepackage;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.io.PrintWriter;

/**

* Call this servlet like this: http://localhost:8080/someservlet

Note that the URL patterns /someservlet should be mapped for somepackage.SomeServlet in /WEB-INF/web.xml

*/

public class SomeServlet extends HttpServlet {

/*

Handle HTTP Post , form is posted with method="post"

*/

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

doGet(request,response);

}

/*

Handle HTTP Get, form is posted with method="get"

*/

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try {

//Write HTML Header

PrintWriter out = response.getWriter();

out.println("<html><head><title>Some Page Title </title></head><body>");

out.println("<b><i>Some Text</i></b>");

//Write other stuff

// Write HTML footer

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

out.flush();

out.close();

} catch (Exception ex) {

//Log errors with Log4J for example

throw new ServletException(ex);

}

}

}

In your projects /WEB-INF/ folder , look for web.xml over there you need to define a mapping for your servlet, something like this.

<?xml version="1.0"?>

<web-app>

<servlet>

<servlet-name>SomeServlet</servlet-name>

<servlet-class>somepackage.SomeServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SomeServlet</servlet-name>

<url-pattern>/someservlet</url-pattern>

</servlet-mapping>

</web-app>

After you compile your Servlet class and add the above servlet name and url mapping entry in your web.xml you'll be able to call it with http://localhost:8080/someservlet

The above example doesn't do exactly what you want, but it's just the basic stuff to get you started, you can complete the remaining parts by yourself (assuming you know Java Classes).

Message was edited by:

appy77

appy77a at 2007-7-10 3:24:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
appy,Thanks, for all your time and advise. I can now work from your example.
kensingtona at 2007-7-10 3:24:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

You're welcome.

Wish you luck!

Just an FYI , if you want to write a JavaBean , treat one instance of JavaBean as 1 record from the database , or simply 1 record of data.

According to the JavaBean notation , a JavaBean is written like this

package somebeanpackage;

import java.io.Serializable;

public class SomeBean implements Serializable{

private String firstName;

public SomeBean(){} // a JavaBean must have a default empty constructor.

public String getFirstName(){

return this.firstName;

}

public void setFirstName(String firstName){

this.firstName = firstName;

}

}

The above is an example of a JavaBean built with standard JavaBean notation. When you learn about Java Reflection, you'll understand why having the above standard notation is useful.

But for now, you could create an ArrayList of any JavaBean , and that ArrayList of JavaBeans could represent multiple records of data obtained from the database via JDBC resultset .

When you iterate over the JDBC resultset you can populate the ArrayList of JavaBeans with the resultset data.

In addition to the standard methods a JavaBean could have other custom methods too.

Message was edited by:

appy77

appy77a at 2007-7-10 3:24:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...