servlet to jsp communications

First we are going to do the connection using hashtables, after that we will do the same thing using java beans instead.

1. create the servlet. name it ServletJSPComm.java and copy the text below or download the files here.
//**************************** code start *********************************************
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;

public class ServletJSPComm extends HttpServlet
{
 public void doGet(HttpServletRequest request, HttpServletResponse response)
                     throws ServletException, IOException
 {
  try
  {
   Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        }
        catch (Exception E)
  {
         System.out.println("Unable to load driver.");
        }                    
  try
  {
   Connection Conn = DriverManager.getConnection("jdbc:mysql://localhost/javatutes?user=username&password=passwd");            
            // Do something with the Connection
            try
   {
    Hashtable links = new Hashtable();
    Hashtable link_parts = new Hashtable();  
    // Use some connection we've already created
                Statement Stmt = Conn.createStatement();
                ResultSet RS = Stmt.executeQuery("SELECT jtutes_users.user_name FROM jtutes_users");
    
    String user_name_str;
    int i = 1;
    
    while (RS.next())
    {
      user_name_str = RS.getString("user_name");
      if (user_name_str == null)
       user_name_str = "unknown"; 
      link_parts.put("user_name", new String(user_name_str));
         links.put("part" + i, new Hashtable(link_parts));
      i++;
    } 
    request.setAttribute("user_name", links);
       
    String url = "/jsp/display_names.jsp";
    ServletContext context = getServletContext();
    RequestDispatcher rd = context.getRequestDispatcher(url);
    rd.forward(request, response);
    
    // Clean up after ourselves
                RS.close();
                Stmt.close();
                Conn.close();
    
    
      }
      catch (SQLException E)
   {
          System.out.println("SQLException: " + E.getMessage());
                System.out.println("SQLState:     " + E.getSQLState());
                System.out.println("VendorError:  " + E.getErrorCode());
            }
  }
        catch (SQLException E)
  {
         System.out.println("SQLException: " + E.getMessage());
   System.out.println("SQLState:     " + E.getSQLState());
   System.out.println("VendorError:  " + E.getErrorCode());
        }
 }
 public String getServletInfo()
    {
  return "Servlet jsp communication";
    }
}
//**************************** code end *********************************************

2. create the jsp for handling the results, name it display_names.jsp. Note the servlet forwards the request to this file using the RequestDispatcher.
//**************************** code start *********************************************
<% 
String user_name_str = null;
java.util.Hashtable user_name;
java.util.Hashtable user_name_items;
user_name = (java.util.Hashtable)request.getAttribute("user_name");
%>
<html>
<head>
 <title>test jsp to servlet communication</title>
</head>
<body>
This data is now getting displayed form the jsp files. This way we can sepeate the
html from the java and make code easier to maintain.<br><br>
<%
for (java.util.Enumeration e = user_name.elements(); e.hasMoreElements();)
{
 user_name_items = (java.util.Hashtable)e.nextElement();
 user_name_str = (String)user_name_items.get("user_name");
 %>
  <br>debug user_name:<b><%=user_name_str%></b>
 <%
}
%>
</body>
</html>
//**************************** code end *********************************************
3. Click here to see it working.

Now is time to use a session bean to pass information between a servlet and jsp file. The session bean will store the information for the entire session so this data can be accessed from any page as long as the session is active.

4. First we need to create a new java bean.
Lets call it UserNameBean.java and put it in a package called jtutes, same file as before here. Put it in a kawa project file of its own and set the class files to build to a directory specified by the user, see the images below.


sorry still to finish this bit ....

Create the servlet to store some info in a session bean then pass the response onto the jsp file. Lets call the new servlet ServletJSPCommBean.java. x. Click here to see it working, now uses the a java session bean to store the data.

last java webhosting next
bl br
copyright