Problems designing a web site template chooser


D

dinesh prasad

I'm trying to set up a template chooser that will allow users to
create a website. It allows them to input information. At the end it
shows them their page.

2 problems:
1) When I link to the servlet from the main page, it
automatically goes
to the website and puts null values into the fields.

2) I have to restart the browser each time or it loads the page
with old info. If i restart
the application server then I don't have to
restart the browser.

I think this is because I need to separate the servlet into different
files. What's the best way to do this? Thanks!

(There are some inconsistencies in this code because I'm re-naming the
fields..and am not finished.. but the basic flow of the program works)

package com.infologic;

import javax.servlet.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

/**
* The ColorSessionServlet demonstrates the session management
* mechanisms built into the servlet API. A session is
* established and the client's preferred background color
* as well as the number of times they have requested the
* servlet are stored.
*/
public class ColorSessionServlet extends HttpServlet
{
Connection dbConn = null;

// create a persistent conneciton to the SQL server

public void init() throws ServletException
{

System.out.println("servlet started");
String jdbcDriver = "weblogic.jdbc.mssqlserver4.Driver";
String dbURL =
"jdbc:weblogic:mssqlserver4:[email protected]";
String usernameDbConn = "dinesh";
String passwordDbConn = "werty6969";

System.out.println("database info set up..");

try
{
Class.forName(jdbcDriver).newInstance();
dbConn = DriverManager.getConnection(dbURL,
usernameDbConn, passwordDbConn);
}
catch (ClassNotFoundException e)
{
throw new UnavailableException("jdbc driver not
found:" + dbURL);
}
catch (SQLException e)
{
throw new UnavailableException("error: " + e);
}
catch (Exception e)
{
throw new UnavailableException("error: " +e);
}
}
/**
* Generates HTML pages that allows the client to choose a
* color and then remembers the color on future requests.
*/
public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
String name;
String hometitle; //name of user
String home;
String hometitle;
String color;
String services; //user's color preference
String servicestitle;
Integer hitCount; //# of times user has requested servlet
String aboutus="";
String aboutustitle;
String pOne="";
String contactus;
String contactustitle;
String misc="";
String misctitle;

//String contact="";

//get current session, create a new one if it doesn't exist
HttpSession mySession = request.getSession(true);

//MIME type to return is HTML
response.setContentType("text/html");

//get a handle to the output stream
PrintWriter out = response.getWriter();

if (mySession.isNew()) //first time client requests page
{
//generate HTML form requesting name and color preference
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Color Selector</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<FORM METHOD=\"POST\" " +
"ACTION=\"color\">");
out.println("Please select background color:<BR>");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"bcolor\" " +
"VALUE=\"white\">White<BR>");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"bcolor\" " +
"VALUE=\"red\">Red<BR>");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"bcolor\" " +
"VALUE=\"green\">Green<BR>");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"bcolor\" " +
"VALUE=\"blue\">Blue<P>");

out.println("Please enter your name:<BR>");
out.println("<INPUT TYPE=\"TEXT\" NAME=\"name\" " +
"SIZE=\"25\"><P>");
out.println("Your password:<br>");
out.println("<input type=\"password\" name=\"pOne\" " +
"size=\"25\"><p>");

out.println("Enter the title of the main page: <br>");
out.println("<input type=\"text\" name=\"hometitle\" " +
"size=\"50\"><p>");

out.println("Enter the text you want displayed on the main page
below:<br>");
out.println("<input type=\"text\" name=\"home\" " +
"size=\"50\"><p>");


out.println("Enter the title of the About us page: <br>");
out.println("<input type=\"text\" name=\"aboutustitle\" " +
"size=\"50\"><p>");

out.println("<b>About Us</b>Give some background information
about your company <br>");
out.println("<input type=\"text\" name=\"aboutus\" " +
"size=\"50\"><p>");



out.println("Enter the title of the services page: <br>");
out.println("<input type=\"text\" name=\"servicestitle\" " +
"size=\"50\"><p>");

out.println("Enter the services and functions of your
organization: <br>");
out.println("<input type=\"text\" name=\"services\" " +
"size=\"50\"><p>");

out.println("Enter the title of the Contact page: <br>");
out.println("<input type=\"text\" name=\"contactustitle\" " +
"size=\"50\"><p>");

out.println("<b>Contact Information</b>:<br>");
out.println("<input type=\"text\" name=\"contactus\" " +
"size=\"50\"><p>");



out.println("<INPUT TYPE=\"SUBMIT\" VALUE=\"Submit\">");
out.println("</BODY>");
out.println("</HTML>");
}
else //client has already established a session
{
if (request.getParameter("bcolor") != null)
{
//client is submitting the color preference form
String bcolor; //for user's preferred background color
bcolor = request.getParameter("bcolor");

//get HEX code for color
if (bcolor.equals("red"))
{
color = "#FF0000";
}
else if (bcolor.equals("green"))
{
color = "#00FF00";
}
else if (bcolor.equals("blue"))
{
color = "#0000FF";
}
else //if nothing selected, default to white
{
color = "#FFFFFF";
}

name = request.getParameter("name"); //get user name
pOne = request.getParameter("pOne");

aboutus = request.getParameter("aboutus");
aboutustitle = request.getParameter("aboutustitle");

hometitle = request.getParameter("hometitle");
home = request.getParameter("home");

services = request.getParameter("services");
servicestitle = request.getParameter("servicestitle");

contactus = request.getParameter("contactus");
contactustitle = request.getParameter("contactustitle");

misc = request.getParameter("misc");
misctitle = request.getParameter("misctitle");
//contact = request.getParamter("contact");

hitCount = new Integer(1); //requested 1 time so far

mySession.setAttribute("bcolor", color); //store color
mySession.setAttribute("hitCount", hitCount);
mySession.setAttribute("name", name); //store user name
mySession.setAttribute("pOne", pOne);

mySession.setAttribute("aboutus", aboutus);
mySession.setAttribute("aboutustitle", aboutustitle);

mySession.setAttribute("services", services);
mySession.setAttribute("servicestitle", servicestitle);

mySession.setAttribute("contactus", contactus);
mySession.setAttribute("contactustitle", contactustitle);

mySession.setAttribute("misc", misc);
mySession.setAttribute("misctitle", misctitle);

mySession.setAttribute("home", home);
mySession.setAttribute("hometitle", hometitle);
//mySession.setAttribute("contact", contact);
}
else //user has previously submitted HTML form
{
//get color, name, and hit count from session
color = (String)mySession.getAttribute("bcolor");
name = (String)mySession.getAttribute("name");
pOne = (String)mySession.getAttribute("pOne");
System.out.println(pOne);

home = (String)mySession.getAttribute("home");
hometitle = (String)mySession.getAttribute("hometitle");

aboutus = (String)mySession.getAttribute("aboutus");
aboutustitle = (String)mySession.getAttribute("aboutustitle");

services = (String)mySession.getAttribute("services");
servicestitle =
(String)mySession.getAttribute("servicestitle");

contactus = (String)mySession.getAttribute("contactus");
contactustitle =
(String)mySession.getAttribute("contactustitle");

misc = (String)mySession.getAttribute("misc");
misctitle = (String)mySession.getAttribute("misctitle");




//tops = (String)mySession.getAttribute("tops");
//bottoms = (String)mySession.getAttribute("bottoms");
//accessories = (String)mySession.getAttribute("accessories");
//contact = (String)mySession.getAttribute("contact");

//hitCount = (Integer)mySession.getAttribute("hitCount");
try
{
//make a callable statement for a stored procedure.
//It has four parameters

System.out.println("sending datacall");
CallableStatement cstmt = dbConn.prepareCall(
"{call storePageValues(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?)}");

//set the values of the stored procedure's input
parameters

//out.println("calling stored procedure . . .");
cstmt.setString(1, color);
cstmt.setString(2, name);
cstmt.setString(3, pOne);
cstmt.setString(4, aboutus);
cstmt.setString(5, aboutustitle);
cstmt.setString(6, home);
cstmt.setString(7, hometitle);
cstmt.setString(8, services);
cstmt.setString(9, servicestitle);
cstmt.setString(10, contactus);
cstmt.setString(11, contactustitle);
cstmt.setString(12, misc);
cstmt.setString(13, misctitle);
//cstmt.setString(8, contact);


//cstmt.setString(5, notes);
//now that the input parameters are set, we can proceed to
execute the insertTheForm stored procedure

cstmt.execute();


out.println("<html>");
out.println("<head>");
out.println("<title>" + name + "s homepage</title>");
out.println("</head>");
out.println("<br>Welcome to " + name + "s website!</br>");
out.println("<a href=about.jsp?aboutus=" + aboutus +
">About Us</a>");
out.println("<p>");
out.println("<a href=tops.jsp?tops=" + tops +
">Tops</a>");
out.println("<p>");
out.println("<a href=bottoms.jsp?bottoms=" + bottoms +
">Bottoms</a>");
out.println("<p>");
out.println("<a href=accessories.jsp?accessories=" +
accessories + ">Accessories</a>");
out.println("<p>");
//out.println("<a href=contact.jsp?contact=" + contact +
">Contact</a>");
out.println("</html>");
out.close();

}

catch (SQLException e)
{
throw new UnavailableException("error: " + e);

}
}

//increment hit count and store in session


out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Color Selected</TITLE>");
out.println("</HEAD>");
out.println("<BODY BGCOLOR=\"" + color + "\">");
out.println("<H2>Hello " + name + "!</H2>");
out.println("About us " + aboutus + "<p>");
out.println("Tops " + tops + "<p>");
out.println("Bottoms " + bottoms + "<p>");
out.println("Accessories " + accessories + "<p>");

out.println("Satisfied with your entries?<BR>");


out.println("<A
HREF=http://localhost:7001/freelance/color>Confirm
Selection</A></H3>");
out.println("</BODY>");
out.println("</HTML>");
}
out.close();
}


/**
* Returns a brief description of this servlet.
*
* @return Brief description of servlet
*/
public String getServletInfo()
{
return "Servlet uses session management to maintain color.";
}
}

-------------------------------------------------------------------

Maybe you should split into a servlet and a JSP.
The JSP could display without all those out.println()'s
2) I have to restart the browser each time or it loads the page with old info. If i restart
the application server then I don't have to
restart the browser
If you put info into session attributes, then that is the expected
behavior.


Comment from dualsoul
Date: 10/18/2003 12:59PM PDT Comment

oh...it's a very poor design, this is very common anti-pattern
'Monolothic Servlet'
You should'nt mix your representation (view) and your logic.
So you should use MVC paradigm, you can find some articles about MVC
in jsp/servlets and read them. (try to search for model2)
So, you should display info only (!!) in jsp, you can provide data
for it through session, or through info bean. JSP page should obtain
it's data from session/bean and display it, according to your web page
design.
Servlet should be used as Controller: analyze input data, run some
calculation according it, and forward to jsp page to display result
(if, you need it).
So, i've took a rapid look for your code, and i suggest you to
split it for:
firsttime.jsp - to display info, when client request first time
result.jsp - to display info, when client submited formm and DB
processing passed
you ColorServlet to handle form submiting. It should forward to
..jsp, when it need to display info.
Hope it'll help you. But if you have questions, please fill free to
ask :)




Comment from dprasad
Date: 10/18/2003 11:26PM PDT Your Comment

Okl I have tried to split things up into beans and JSP. When the user
views the page, the values need to come from the database. Note that I
commented out the session.invalidate() method at the end of the
registration servlet because I thought it might be interrupting the
session. It still does the same thing either way.
I'm now getting a JSP run-time error, its complaining about the
iterator, here's the error followed by the code. Note that on the
homepage.jsp I'm calling all of the fields, but they will be called
from separate JSP's ultimately..

Here's the error:

C:\bea\user_projects\infologic1\.\myserver\.wlnotdelete\extract\myserver_freelance_freelance\jsp_servlet\__homepage.java:205:
'catch' without 'try'
(No more information available, probably caused by another error)

C:\bea\user_projects\infologic1\.\myserver\.wlnotdelete\extract\myserver_freelance_freelance\jsp_servlet\__homepage.java:152:
'try' without 'catch' or 'finally'
(No more information available, probably caused by another error)

C:\bea\user_projects\infologic1\.\myserver\.wlnotdelete\extract\myserver_freelance_freelance\jsp_servlet\__homepage.java:215:
'}' expected
(No more information available, probably caused by another error)

C:\bea\user_projects\infologic1\.\myserver\.wlnotdelete\extract\myserver_freelance_freelance\jsp_servlet\__homepage.java:176:
cannot resolve symbol
probably occurred due to an error in /homepage.jsp line 12:
Iterator iterator = showUserBeans.iterator();


<%@page contentType="text/html"%>
<%@page import="com.infologic.showUserPageBean" %>
<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<jsp:useBean id="dbBean" scope="session"
class="com.infologic.showPageBean"/>
<html>

<%
String name = request.getParameter("Username");

ArrayList showUserPageBeans = dbBean.getWebPageInfo(name);
Iterator iterator = showUserBeans.iterator();
while (iterator.hasNext()) {

showUserPageBean userbean = (showUserPageBean) iterator.next();


%>
<head><title><%=userbean.hometitle%></title></head>
<body>

<table>
<tr>
<td><b>Welcome..</b></td>
<td><b><%=userbean.home%></td></b>

<%=userbean.name%><p>
<%=userbean.aboutus%><p>
<%=userbean.aboutustitle%><p>
<%=userbean.contactus%><p>
<%=userbean.contactustitle%><p>
<%=userbean.misc%><p>
<%=userbean.misctitle%> <p>
<%=userbean.services%><p>
<%=userbean.servicestitle%><p>


</tr>

<%-- <jsp:useBean id="beanInstanceName" scope="session"
class="package.class" /> --%>
<%-- <jsp:getProperty name="beanInstanceName" property="propertyName"
/> --%>

</body>
</html>


package com.infologic;


/*
* formHandlingServlet.java
*
* Created on July 6, 2003, 7:01 PM
*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.text.DateFormat;


/**
*
* @author Administrator
*/
public class registerNewUserServlet extends HttpServlet {

//private static final String email1 = "email";
//private static final String password1 = "password1";
//private static final String password2 = "password2";
//private static final String displayname = "displayname";

Connection dbConn = null;

// create a persistent conneciton to the SQL server

public void init() throws ServletException
{


System.out.println("servlet started");
String jdbcDriver = "weblogic.jdbc.mssqlserver4.Driver";
String dbURL =
"jdbc:weblogic:mssqlserver4:[email protected]";
String usernameDbConn = "dinesh";
String passwordDbConn = "werty6969";

System.out.println("database info set up..");

try
{
Class.forName(jdbcDriver).newInstance();
dbConn = DriverManager.getConnection(dbURL,
usernameDbConn, passwordDbConn);
}
catch (ClassNotFoundException e)
{
throw new UnavailableException("jdbc driver not
found:" + dbURL);
}
catch (SQLException e)
{
throw new UnavailableException("error: " + e);
}
catch (Exception e)
{
throw new UnavailableException("error: " +e);
}
}






public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
// HttpSession mySession = request.getSession(true);


//if (mySession.isNew()) //first time client requests page
// {
//public Hashtable getCategories() {
HttpSession mySession = request.getSession(true);
response.setContentType("text/plain");
String name;
String pass;
String hometitle;
String home;

String color;
String services; //user's color preference
String servicestitle;
Integer hitCount; //# of times user has requested servlet
String aboutus="";
String aboutustitle;
String pOne="";
String contactus;
String contactustitle;
String misc="";
String misctitle;
PrintWriter out = response.getWriter();

//extract parameter information from register.jsp

System.out.println("setting request parameters!");


//String mailboxnumber =
request.getParameter("mailboxnumber");

/*if (action.equals("search"))
url = base + "SearchResults.jsp";
else if (action.equals("editcustomer"))
if (action!=null) {*/



name = request.getParameter("Username"); //get user name
pass = request.getParameter("pass");

aboutus = request.getParameter("aboutus");
aboutustitle = request.getParameter("aboutustitle");

hometitle = request.getParameter("maintitle");
home = request.getParameter("main");

services = request.getParameter("services");
servicestitle = request.getParameter("servicestitle");

contactus = request.getParameter("contactus");
contactustitle = request.getParameter("contactustitle");

misc = request.getParameter("misc");
misctitle = request.getParameter("misctitle");


//mySession.setAttribute("bcolor", color); //store color
//mySession.setAttribute("hitCount", hitCount);
mySession.setAttribute("name", name); //store user name
mySession.setAttribute("pOne", pOne);

mySession.setAttribute("aboutus", aboutus);
mySession.setAttribute("aboutustitle", aboutustitle);

mySession.setAttribute("services", services);
mySession.setAttribute("servicestitle", servicestitle);

mySession.setAttribute("contactus", contactus);
mySession.setAttribute("contactustitle", contactustitle);

mySession.setAttribute("misc", misc);
mySession.setAttribute("misctitle", misctitle);

mySession.setAttribute("home", home);
mySession.setAttribute("hometitle", hometitle);


try
{
//make a callable statement for a stored procedure.
//It has four parameters

System.out.println("sending datacall");
CallableStatement cstmt = dbConn.prepareCall(
"{call storePageValues(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?)}");

//set the values of the stored procedure's input
parameters

out.println("calling stored procedure . . .");
cstmt.setString(1, name);
cstmt.setString(2, pass);
cstmt.setString(3, aboutus);
cstmt.setString(4, aboutustitle);
cstmt.setString(5, services);
cstmt.setString(6, servicestitle);
cstmt.setString(7, contactus);
cstmt.setString(8, contactustitle);
cstmt.setString(9, misc);
cstmt.setString(10, misctitle);
cstmt.setString(11, home);
cstmt.setString(12, hometitle);

//cstmt.setString(5, notes);
//now that the input parameters are set, we can proceed to
execute the insertTheForm stored procedure

cstmt.execute();
out.println("stored procedure executed");
out.close();
}

catch (SQLException e)
{
throw new UnavailableException("error: " + e);

}
//mySession.invalidate();
System.out.println("Session has been invalidated");
response.sendRedirect("http://localhost:7001/freelance/homepage");
}



}

package com.infologic;

/**
*
* @author Administrator
*/
public class showUserPageBean {

public String name;
public String pass;
public String hometitle;
public String home;

public String color;
public String services; //user's color preference
public String servicestitle;
public Integer hitCount; //# of times user has requested servlet
public String aboutus="";
public String aboutustitle;
public String pOne="";
public String contactus;
public String contactustitle;
public String misc="";
public String misctitle;
}

/*
* showPageBean.java
*
* Created on January 1, 2000, 7:19 AM
*/

package com.infologic;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.text.DateFormat;
import java.util.*;
import com.infologic.showUserPageBean;


/**
*
* @author Administrator
*/
public class showPageBean {

Connection dbConn = null;

// create a persistent conneciton to the SQL server

public void init() throws ServletException
{

System.out.println("servlet started");
String jdbcDriver = "weblogic.jdbc.mssqlserver4.Driver";
String dbURL =
"jdbc:weblogic:mssqlserver4:[email protected]";
String usernameDbConn = "dinesh";
String passwordDbConn = "werty6969";

System.out.println("database info set up..");

try
{
Class.forName(jdbcDriver).newInstance();
dbConn = DriverManager.getConnection(dbURL,
usernameDbConn, passwordDbConn);
}
catch (ClassNotFoundException e)
{
throw new UnavailableException("jdbc driver not
found:" + dbURL);
}
catch (SQLException e)
{
throw new UnavailableException("error: " + e);
}
catch (Exception e)
{
throw new UnavailableException("error: " +e);
}
}
public ArrayList getWebPageInfo(String username) {
ArrayList showUserPageBeans = new ArrayList();

try
{
Statement s = dbConn.createStatement();
String sql = "select name1, aboutus, aboutustitle, contactus,
contactustitle, home, hometitle, misc, misctitle, services,
servicestitle from pagevalues where name1=" + username;
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
showUserPageBean userbean = new showUserPageBean();
userbean.name = rs.getString(1);
userbean.aboutus = rs.getString(2);
userbean.aboutustitle = rs.getString(3);
userbean.contactus = rs.getString(4);
userbean.contactustitle = rs.getString(5);
userbean.home = rs.getString(6);
userbean.hometitle = rs.getString(7);
userbean.misc = rs.getString(8);
userbean.misctitle = rs.getString(9);
userbean.services = rs.getString(10);
userbean.servicestitle = rs.getString(11);

}
rs.close();
s.close();

}
catch (SQLException e) {}
return showUserPageBeans;
}




}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top