J2EE getParameter() Problem

K

KK

Hello,

I am having a problem with getParameter(). This is for my Masters
Class.

I have a JSP that forwards requests to a Servlet. The application
displays quotes. The user can add quotes, delete quotes, and naviagte
through the quotes. All of the "single" button requests work ("DEl",
"NExt" and "Back") it is the multiple parameter requests that do not
work.

Here is my Servlet:

/*
*ks02HW2
* QuoteServlet.java
*
* Created on October 8, 2003, 9:42 AM
*/

import java.lang.Object.*;
import java.io.*;
import java.net.*;
import info.QuoteBean;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.*;
import org.xml.sax.*;


/** //== forward to JSP
ServletContext context = getServletContext();
RequestDispatcher rd =
context.getRequestDispatcher("/target.jsp");
rd.forward(request, response);
*
* @author ksmith
* @version
*/

public class QuoteServlet extends HttpServlet {

/** Initializes the servlet.
*/
private QuoteBean[] StringOfBeans = null;
private QuoteBean[] TempStringOfBeans = null;
public int size = 5;
public int last = size-1;

/** create an int to hold the index of the current postion
*/
public int position = 0;

public void init() throws ServletException {
super.init();

//== create beans and add them to the array
StringOfBeans = new QuoteBean[size];

//== Set the beansArray postion with quotes
StringOfBeans[0] = new QuoteBean();
StringOfBeans[0].setQuoteAuthor( "Guy in Bar" );
StringOfBeans[0].setQuoteDate( "10/4/2003" );
StringOfBeans[0].setQuote( "Do you mind if we dance with your
dates?" );

StringOfBeans[1] = new QuoteBean();
StringOfBeans[1].setQuote( "Was it over when the Germans
bombed Pearl Harbor?" );
StringOfBeans[1].setQuoteAuthor( "Bluto" );
StringOfBeans[1].setQuoteDate( "1979" );

StringOfBeans[2] = new QuoteBean();
StringOfBeans[2].setQuote( "Excuse me sir... is this the the
Delta House?");
StringOfBeans[2].setQuoteAuthor( "Flounder" );
StringOfBeans[2].setQuoteDate( "1972" );

StringOfBeans[3] = new QuoteBean();
StringOfBeans[3].setQuote( "Thank you sir, may I have
another?");
StringOfBeans[3].setQuoteAuthor( "Chip" );
StringOfBeans[3].setQuoteDate( "1972" );

StringOfBeans[4] = new QuoteBean();
StringOfBeans[4].setQuote( "As of now, you are on double
secret probation");
StringOfBeans[4].setQuoteAuthor( "Stork" );
StringOfBeans[4].setQuoteDate( "1972" );

}

/** Destroys the servlet.
*/
public void destroy() {

}

// Needed to over right doPost with doGet
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
doGet(request, response);
}


/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* time to make the donuts
*this the another time to take down the rest of the stuff...
*/

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

/**Create three strings that will hold the values of the string
request.
*The backButton
*/
String next = request.getParameter("next");
String back = request.getParameter("back");
String delete = request.getParameter("del");
String add = request.getParameter("Add");


/** Check to see if this is the first time that the
* user has requested the page. If so create a session.
*If not check to see what button the user has pressed. Then
process
*the request based on what button the user presses.
*/
HttpSession session = request.getSession(false);
try
{
//no session
if (session == null ) {
request.setAttribute("quote",
StringOfBeans[position]);
}
//next button
else if (next != null && position != last) {
position = position + 1;
request.setAttribute("quote",
StringOfBeans[position]);
}
//next button but end of array. set to begining
else if (next != null && position == last) {
position = 0;
request.setAttribute("quote",
StringOfBeans[position]);
}
//back button
else if (back != null && position != 0) {
position = position - 1;
request.setAttribute("quote",
StringOfBeans[position]);
}
//back button but at the begining. set to the end
else if (position == 0 && back != null ) {
position = last;
request.setAttribute("quote",
StringOfBeans[position]);
}


// delete button
else if (delete != null)
{
// x will hold the value of each position that is
added to the array
int x = 0;
// TempStringOfBeans will be a temp Array while the
remaining values are added
TempStringOfBeans = new QuoteBean[(size-1)];
// for loop to go through the size of the current
Array
for (int i = 0; i<size ; i++)
{
// add the bean to the temp array if the current
position is not the one deleted
if ( i != position){
TempStringOfBeans[x] = new QuoteBean();

TempStringOfBeans[x].setQuote(StringOfBeans.getQuoteText());

TempStringOfBeans[x].setQuoteAuthor(StringOfBeans.getQuoteAuthor());

TempStringOfBeans[x].setQuoteDate(StringOfBeans.getQuoteDate());
x++;
}
}
// Set the temp to the StringOfBeans
StringOfBeans = TempStringOfBeans;
//Null the TempStringOfBeans
TempStringOfBeans = null;
//Decrease the size of the Array for navigation
size = size-1;
//Decrease the last position of the array
last = last-1;
//Set the current postion to the first item in the
array and display it
position = 0;
request.setAttribute("quote",
StringOfBeans[position]);

}
//Add Button
else if(add != null)
{
// x will hold the value of each position that is
added to the array
TempStringOfBeans = new QuoteBean[(size+1)];
for (int i = 0; i<size ; i++)
{
// Put the original values into the Array[]

TempStringOfBeans = new QuoteBean();

TempStringOfBeans.setQuote(StringOfBeans.getQuoteText());

TempStringOfBeans.setQuoteAuthor(StringOfBeans.getQuoteAuthor());

TempStringOfBeans.setQuoteDate(StringOfBeans.getQuoteDate());
}


// Add the Added values to the last item in the
Array


TempStringOfBeans[(size+1)].setQuote(request.getParameter("QuoteText"));

TempStringOfBeans[(size+1)].setQuoteAuthor(request.getParameter("QuoteAuthor"));

TempStringOfBeans[(size+1)].setQuoteDate(request.getParameter
("QuoteDate"));

// Set the temp to the StringOfBeans
StringOfBeans = TempStringOfBeans;
//Null the TempStringOfBeans
TempStringOfBeans = null;
//Increase the size of the Array for navigation
size = size+1;
//Increase the last position of the array
last = last+1;
//Set the current postion to the first item in the
array and display it
position = 0;
request.setAttribute("quote",
StringOfBeans[position]);
}
}


catch (Exception e){
e.printStackTrace();
}




ServletContext context = getServletContext();
RequestDispatcher rd =
context.getRequestDispatcher("/QuotesJSP.jsp");
rd.forward(request, response);
}

}




Here is the JSP:

<%@page contentType="text/html"%>
<%@ page import="java.util.*" %>
<html>
<LINK rel='stylesheet' type='text/css' href='/Style.css' />
<head><title>HW2</title>
<SCRIPT language='javascript'>
function add_record(id){

var rows="table" + id;
if (document.getElementById(rows).className=="ShowList"){

document.getElementById(rows).className="HideLists";
document.getElementById(id).innerHTML="<font face='Webdings'
size='2'>4</font> Add quote"
}
else{
document.getElementById(rows).className="ShowList";
document.getElementById(id).innerHTML="<font face='Webdings'
size='2'>6</font> Hide add";
}

}
function delete_record(id){

var rows="table" + id;
if (document.getElementById(rows).className=="ShowList"){

document.getElementById(rows).className="HideLists";
document.getElementById(id).innerHTML="<font face='Webdings'
size='2'>4</font> Delete quote"
}
else{
document.getElementById(rows).className="ShowList";
document.getElementById(id).innerHTML="<font face='Webdings'
size='2'>6</font> Hide delete";
}

}
</SCRIPT>

</head>
<body>
<TABLE>
<TR>
<TD width='30%' valign='top'>
<p><u><b><font size="2" color="red">Java Project # 2
</font></b></u></p>

<P class='text'><b>Student:</b></P>

<P class='text'><b>Ogan Sayek os05 <br />Kevin Smith ks02</b></P>

</TD>
<TD width='100%' align='center'>

<DIV align='center'>
<P>These Quotes are taken from the movie Animal House</P>
<TABLE width='500' align='center' class='frame' bgcolor='#FFFFCC'>




<FORM method='get' action='QuoteServlet'
<TR>
<TD id='back' class='Text'>
<input type='submit' value='Back' name='back'>
</TD>
<TD class='Text' align='right'>
<input type="submit" value="Next" name="next">
</TD>
</TR>

</FORM>


<TR>
<TD colspan='2'>
<TABLE width='500' class='container' bgcolor='white'>
<TR>
<TD colspan='2' bgcolor='wheat'>
<P><FONT color='blue'>Animal House Quotes

</FONT></P>
</TD>
</TR>
<TR>
<TD colspan='2'>
<BR />
<I>
"
<jsp:useBean id="quote"
class="info.QuoteBean" scope="session" />
<jsp:getProperty name="quote"
property="quoteText" />
"
</I>
</TD>
</TR>
<TR>
<TD width='30%'></TD>
<TD align='right'>
<P class='Text'>Date:
<jsp:useBean id="Date"
class="info.QuoteBean" scope="session" />
<jsp:getProperty name="quote"
property="quoteDate" /></P>
</TD>
</TR>
<TR>
</P><TD width='30%'></TD>
<TD align='right'>
<P class='Text'> Author:
<jsp:useBean id="Author"
class="info.QuoteBean" scope="session" />
<jsp:getProperty name="quote"
property="quoteAuthor" />
</P>
</TD>
</TR>
<TR>
<TD colspan='2'>
<P style='cursor:pointer' class='text' id='1'
onclick='add_record(1)'><font face='Webdings' size='2'>4</font> Add
quote</P>
</TD>
</TR>
<TR>
<TD colspan='2'>
<TABLE width='100%' id='table1' class="HideLists"
bgcolor='wheat'>
<form method="get" action="QuoteServlet">
<TR>
<TD class='header'>
<B>Add quote</B>
</TD>
</TR>
<TR>
<TD>
<P class='text'>Quote:</P>
</TD>
<TD>
<P class='text'><input type="text"
name="QuoteText" size="50"></P>
</TD>
</TR>
<TR>
<TD>
<P class='text'>Date:</P>
</TD>
<TD>
<P class='text'><input type="text"
name="QuoteDate" size="20"></P>
</TD>
<TR>
<TD>
<P class='text'>Author:</P>
</TD>
<TD>
<P class='text'><input type="text"
name="QuoteAuthor" size="20"></P>
</TD>
</TR>
<TR>
<TD colspan='2'>
<P class='text'><input type="submit"
value="Add" name="Add"></P>
</TD>
</TR>
</form>
</TABLE>
</TD>
</TR>
<TR>
<TD colspan='2'>
<P style='cursor:pointer' class='text' id='2'
onclick='delete_record(2)'><font face='Webdings' size='2'>4</font>
Delete quote</P>
</TD>
</TR>
<TR>
<TD colspan='2'>
<TABLE width='100%' id='table2' class="HideLists"
bgcolor='wheat'>
<form method="get" action="QuoteServlet">
<TR><TD class='header'><B>Delete
quote</B></TD></TR>
<TR>
<TD>
<P class='text'>Click the button to
delete this recored (no turning back)</P>
</TD>
<TD>
<P class='text'><input type="submit"
name="del" value="Delete"></P>
</TD>
</TR>
</form>
</TABLE>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</DIV>
<TR>
<TD>
</TABLE>
</body>
</html>


Thanks for your help

Kevin Smith

Lutherville, MD
 
S

ShadowMan

KK said:
Hello, [cut]

Here is my Servlet:

/*
*ks02HW2
* QuoteServlet.java
*
* Created on October 8, 2003, 9:42 AM
*/

import java.lang.Object.*;
import java.io.*;
import java.net.*;
import info.QuoteBean;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.*;
import org.xml.sax.*;


/** //== forward to JSP
ServletContext context = getServletContext();
RequestDispatcher rd =
context.getRequestDispatcher("/target.jsp");
rd.forward(request, response);
*
* @author ksmith
* @version
*/

public class QuoteServlet extends HttpServlet {

/** Initializes the servlet.
*/
private QuoteBean[] StringOfBeans = null;
private QuoteBean[] TempStringOfBeans = null;
public int size = 5;
public int last = size-1;

/** create an int to hold the index of the current postion
*/
public int position = 0;
[cut]
Be attention use member variable for current-position holding...this servlet
is multithread!!
This is not the cause of your problem..it is just an hint
 
K

KK

Yes! Good point. I will correct the position variable. Thanks!

Any ideas on why that add button does not work?

ShadowMan said:
KK said:
Hello, [cut]

Here is my Servlet:

/*
*ks02HW2
* QuoteServlet.java
*
* Created on October 8, 2003, 9:42 AM
*/

import java.lang.Object.*;
import java.io.*;
import java.net.*;
import info.QuoteBean;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.parsers.*;
import org.xml.sax.*;


/** //== forward to JSP
ServletContext context = getServletContext();
RequestDispatcher rd =
context.getRequestDispatcher("/target.jsp");
rd.forward(request, response);
*
* @author ksmith
* @version
*/

public class QuoteServlet extends HttpServlet {

/** Initializes the servlet.
*/
private QuoteBean[] StringOfBeans = null;
private QuoteBean[] TempStringOfBeans = null;
public int size = 5;
public int last = size-1;

/** create an int to hold the index of the current postion
*/
public int position = 0;
[cut]
Be attention use member variable for current-position holding...this servlet
is multithread!!
This is not the cause of your problem..it is just an hint
 

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

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top