DOC

JSP - FAQ (2)

By Antonio Morgan,2014-09-16 16:54
8 views 0
JSP - FAQ (2)

    JSP - FAQ (2)

作者;jspfuns

日期;2000-8-10 17:49:53

10) How do you invoke a JSP page from a servlet? TOC

(Contributed by: Thomas-Bernhard.O-Hare@Dresdner-Bank.com)

    After scanning through archives of the JSP mailing list to no effect I finally remembered that I'd pasted this example into a document I wrote. It was originally sent by Satish Dharmaraj of Sun to show the model 2 approach (as described in the 0.92 specification): how to pass data from a servlet

to a JSP.

    Create a directory called model1/ under the samples/ directory. Place foo.jsp and Foo.java inside this directory.

    Compile FooServlet.java and place FooServlet.class in TOP/servlets/directory.

Then invoke using http://host:8080/servlet/FooServlet

In this example, FooServlet creates a list and then stores the

    result in Foo.class. Foo.Class is then passed as a datasource to

    foo.jsp.

The sources are:

1) FooServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import model1.Foo;

public class FooServlet extends HttpServlet

{

public void service(HttpServletRequest req, HttpServletResponse

    res)

throws ServletException, IOException

{

String s[] = new String[] {"blue", "green", "red"};

Foo f = new Foo(s);

req.setAttribute("foo", f);

getServletContext().getRequestDispatcher("/samples/model1/foo.jsp"

    ).forward

(req, res);

}

}

2) foo.jsp

3) Foo.java

package model1;

public class Foo {

String s[];

public String[] getList() { return s; }

public Foo(String s[]) { this.s = s; }

}

(from "Oare, Thomas Bernhard" <Thomas-Bernhard.O-Hare@Dresdner-

    Bank.com>)

    11) How do you pass data (including beans) to a JSP from a servlet? TOC

    There are actually three different ways to do it, depending on how long the reference should last, and which JSP pages (and servlets,

    for that matter) should be able to see it. In each of the cases, assume that "myBean" is a reference to the bean you want to send, and that "theBean" is the key I'm going to use to store the bean under (from the servlet perspective), and use as the identity of

    the bean in the JSP page.

    These techniques are portable to any environment compliant with the servlet API 2.1 and JSP 1.0 specifications. In each case, the passing

works from servlet->JSP, servlet->servlet, JSP->JSP, or JSP-

    >servlet transitions.

(1) Request Lifetime

    Use this technique to pass beans that are relevant to this particular request to a bean you are calling through a request dispatcher (using either "include" or "forward"). This bean will disappear after processing this request has been completed.

SERVLET:

request.setAttribute("theBean", myBean);

RequestDispatcher rd =

getServletContext().getRequestDispatcher('/thepage.jsp");

rd.forward(request, response);

JSP PAGE:

/>

(2) Session Lifetime

    Use this technique to pass beans that are relevant to a particular session (such as in individual user login) over a number of requests. This bean will disappear when the session is invalidated or it times out, or when you remove it.

SERVLET:

HttpSession session = request.getSession(true);

session.putValue("theBean", myBean);

/* You can do a request dispatcher here,

or just let the bean be visible on the

next request */

JSP PAGE:

<jsp:useBean id="theBean" scope="session" class="..." />

(3) Application Lifetime

    Use this technique to pass beans that are relevant to all servlets and JSP pages in a particular app, for all users. For example, I use this to make a JDBC connection pool object available to the

    various servlets and JSP pages in my apps. This bean will disappear when the servlet engine is shut down, or when you remove it.

SERVLET:

getServletContext().setAttribute("theBean", myBean);

JSP PAGE:

" scope="application" class="..." />

Craig McClanahan

12) How can I pool connections to my database? TOC

Controlling connections to the database is a desirable thing -

    having to connect to the database for each page is is expensive, and keeping a connection in a session variable is far too expensive in terms of client connections to the database. Thus,

    people often create pools for connections to the database that the client comes in and gets and then returns when complete (making sure a try/catch is used to ensure the connection is returned!).

    My personal bias indicates that you shouldn't pool connections to your database inside JSP, you should be using a middleware layer and communicating to it (like RMI or CORBA). However, people do write entire applications in JSP and beans that reside in the web server, so how do you do it?

From: Bradley Wood

    probably instantiate this as a singleton in the global.jsa and then call it.

    for more on this and how its called check the downloadable code examples for this book at http://www.ora.com

import java.sql.*;

import java.util.*;

public class ConnectionPool {

private Hashtable connections;

private int increment;

private String dbURL, user, password;

public ConnectionPool(String dbURL,

String user,

String password,

String driverClassName,

int initialConnections,

int increment)

// int max?

    throws SQLException, ClassNotFoundException {

// Load the specified driver class

Class.forName(driverClassName);

this.dbURL = dbURL;

this.user = user;

this.password = password;

this.increment = increment;

connections = new Hashtable();

    // Put our pool of Connections in the Hashtable

    // The FALSE value indicates they're unused

    for(int i = 0; i < initialConnections; i++) {

connections.put(DriverManager.getConnection(dbURL, user,

    password),

Boolean.FALSE);

}

}

    public Connection getConnection() throws SQLException {

Connection con = null;

Enumeration cons = connections.keys();

synchronized (connections) {

while(cons.hasMoreElements()) {

con = (Connection)cons.nextElement();

Boolean b = (Boolean)connections.get(con);

if (b == Boolean.FALSE) {

// So we found an unused connection.

    // Test its integrity with a quick setAutoCommit(true) call.

    // For production use, more testing should be performed,

// such as executing a simple query.

try {

con.setAutoCommit(true);

}

catch(SQLException e) {

// Problem with the connection, replace it.

    con = DriverManager.getConnection(dbURL, user, password);

}

    // Update the Hashtable to show this one's taken

connections.put(con, Boolean.TRUE);

// Return the connection

return con;

} // if

} // while

} // synchro

    // If we get here, there were no free connections.

// We've got to make more.

for(int i = 0; i < increment; i++) {

connections.put(DriverManager.getConnection(dbURL, user,

    password),

Boolean.FALSE);

}

    // Recurse to get one of the new connections.

return getConnection();

}

Report this document

For any questions or suggestions please email
cust-service@docsford.com