Exchanging Data Between Servlets

Hi,

Im attempting to make certain parts of a website remotely updatable using servlets.

Currently Ive got problems with one part of the system, editing content.

A servlet (EditLink.java shown below) reads plain text line by line from a file, each line is composed of URLText, URL

E.g:

The DVD Forums, www.thedvdforums.com

Microsoft, www.microsoft.com

Each URL and its URL text from the file is displayed on screen with two buttons next to them (EDIT and DELETE) the number of urls on the page can vary. When the servlet is executed the buttons are assigned a name depending on the URL theyre next to, EG The first buttons are called EDIT1 and DELETE1 seventh set are EDIT7 and DELETE7.

Once a button is pressed the information is passed to a second servlet (DoEditLink.java also displayed below) this servlet reads the information sent from the form and attempts to determine which button was pressed. This is where everything goes screwy, the servlet compiles, tomcat resets perfectly, but when its is called on the server the following error message occurs:

java.lang.NullPointerException

at java.lang.StringBuffer.(StringBuffer.java:129)

at DoEditLink.doGet(DoEditLink.java:25)

It seems to be related to the line:

StringBuffer tempStringBuffer = new StringBuffer(DOEDIT);

So my question is can anyone figure this out, or suggest a better way of doing this (Im sure there is but I cant think of it, probably only involving one servlet!). Sorry about the length of this post.

Here some links to the files shown below.

http://www.uea.ac.uk/~w0049336/JavaHelp/EditLink.Java

http://www.uea.ac.uk/~w0049336/JavaHelp/DoEditLink.Java

http://www.uea.ac.uk/~w0049336/JavaHelp/tomcat.bmp ScreenGrab of Tomcat Error

http://www.uea.ac.uk/~w0049336/JavaHelp/TomcatError.doc Word Document

Many thanks in advance!

CJ

EditJava.java:

[code]

import java.util.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class EditLink extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

String LinkContent;

int numberOf = 0;

//open the file

FileReader fr = new FileReader("ForLinksPage.Links");

BufferedReader br = new BufferedReader(fr);

FileReader fr2 = new FileReader("ForLinksPage.Links");

BufferedReader br2 = new BufferedReader(fr2);

res.setContentType("text/html");

PrintWriter out = res.getWriter();

out.println("<HTML>");

out.println("<script language=\"javascript\" type=\"text/javascript\" src=\"http://profprac2.sys.uea.ac.uk/Groupkk/New/top.htm\"></script>");

out.println("<H2><FONT FACE=\"Trebuchet MS\">Edit Links </FONT></H2>");

out.println("<CENTER><HR></CENTER>");

out.println("<FORM METHOD=GET ACTION=\"/groupkk/servlet/DoEditLink\">");

out.println("<FONT FACE=\"ARIAL\">Please Alter a link then press the Edit or Delete button<P>");

while ((LinkContent = br2.readLine()) !=null){

StringTokenizer numSt = new StringTokenizer(LinkContent,",");

String LinkDescription = numSt.nextToken();

String Link = numSt.nextToken();

numberOf++;

}

out.println("<INPUT TYPE=HIDDEN NAME = NUMBER VALUE=\""+ numberOf + "\">");

int currentNumber=1;

numberOf++;

LinkContent = new String();

while ((LinkContent = br.readLine()) !=null){

StringTokenizer st = new StringTokenizer(LinkContent,",");

String LinkDescription = st.nextToken();

String Link = st.nextToken();

out.println("URL Description");

out.println("<INPUT TYPE=TEXT NAME=\"URLText"+currentNumber+"\" VALUE=\"" + LinkDescription + "\">");

out.println("URL");

out.println("<INPUT TYPE=TEXT NAME=\"URL"+currentNumber+"\" VALUE=\"" + Link + "\">");

out.println("<INPUT TYPE=SUBMIT NAME = \"EDIT"+currentNumber+"\" VALUE =\"Edit Link\">");

out.println("<INPUT TYPE=SUBMIT NAME = \"DELETE"+currentNumber+"\" VALUE =\"Delete Link\">");

out.println("<HR>");

currentNumber++;

}

out.println("</FONT></FORM>");

}

}

[/CODE]

DoEditLink.java:

[CODE]

import java.io.*;

import java.lang.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class DoEditLink extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

res.setContentType("text/html");

PrintWriter out = res.getWriter();

String StrNumberOfURLS=req.getParameter("NUMBER");

int NumberOfURLS = Integer.parseInt( StrNumberOfURLS );

String EDITLINK = "Edit+Link";

String DELETELINK = "Delete+Link";

out.println("<HTML>");

out.println("<script language=\"javascript\" type=\"text/javascript\" src=\"http://profprac2.sys.uea.ac.uk/Groupkk/New/top.htm\"></script>");

out.println("<H2><FONT FACE=\"Trebuchet MS\">Edit Links </FONT></H2>");

out.println("<CENTER><HR></CENTER>");

out.println("Number Of URLS = " + NumberOfURLS);

String DOEDIT=req.getParameter("EDIT");

StringBuffer tempStringBuffer = new StringBuffer(DOEDIT);

int currentNum=0;

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

currentNum++;

//

//

tempStringBuffer.append(String.valueOf(currentNum));

DOEDIT = tempStringBuffer.toString( );

//DOEDIT.append(String.valueOf(currentNum));

out.println("DOEDIT? = " + DOEDIT);

out.println("EDITLINK? = " + EDITLINK);

//String DODELETE=req.getParameter("DELETE");

//StringBuffer tempStringBuffer2 = new StringBuffer(DODELETE);

//tempStringBuffer2.append(String.valueOf(currentNum));

//DODELETE = tempStringBuffer2.toString( );

if (DOEDIT.compareTo(EDITLINK) ==0){

String URLText =req.getParameter("URLText"+i);

out.println("Edit " + URLText);

}

//else if (compareTo"Delete+Link" DODELETE)=0){

}

out.println("</HTML>");

out.close();

}

}

[/CODE]

[6636 byte] By [CJCJ] at [2007-9-19]
# 1

Hi, the problem seems to be in these lines:

String DOEDIT = req.getParameter("EDIT");

StringBuffer tempStringBuffer = new StringBuffer(DOEDIT);

The problem is ... parameter "EDIT" is null. The reason it is null is because you are not creating a parameter "EDIT" in the EditLink servlet form ... you are in fact creating:

<INPUT TYPE=SUBMIT NAME = \"EDIT"+currentNumber"\" ...

Your parameter will be something like "Edit1" or "Edit2", but not plain old "Edit" when it submits. You were then looking for a parameter in the DoEditLink servlet named simply "Edit", and that was the problem.

Try using a hidden field in the form to also send along this "currentNumber" as its own parameter. Then you can concatenate "Edit" + req.getParameter("currentNumber"); back into a String that will in fact represent one of the parameter names you have sent yourself. (like Edit1 ...Edit2 etc.)

Hope this helps, it was crashing when you tried to initialize the StringBuffer with a value of null.

>

GumB. at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2

Reposted with line returns so we can read this response :-)

Hi, the problem seems to be in these lines:

String DOEDIT = req.getParameter("EDIT");

StringBuffer tempStringBuffer = new StringBuffer(DOEDIT);

The problem is ... parameter "EDIT" is null. The reason it is null is because you are not

creating a parameter "EDIT" in the EditLink servlet form ... you are in fact creating:

<INPUT TYPE=SUBMIT NAME = \"EDIT"+currentNumber"\" ...

Your parameter will be something like "Edit1" or "Edit2", but not plain old "Edit" when it

submits. You were then looking for a parameter in the DoEditLink servlet named simply

"Edit", and that was the problem.

Try using a hidden field in the form to also send along this "currentNumber" as its own

parameter. Then you can concatenate "Edit" + req.getParameter("currentNumber");

back into a String that will in fact represent one of the parameter names you have sent

yourself. (like Edit1 ...Edit2 etc.)

Hope this helps, it was crashing when you tried to initialize the StringBuffer with a value

of null.

>

GumB. at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3

Actually, after thinking about this a little more, you might want to create a javascript

function that is triggered by any of the 'list' button pushes, and then packs that button's

values into one named parameter. You will only ever push one button at a time, right?

That would be the way to implement a many -> one -- button -> parameter relationship.

Another idea might be to investigate storing the result of a button push in the session

layer. Anyway, I just had more ideas and I thought I would share them with you.

GumB. at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi,

have you set a mapping for your servlet in the web.xml?

I once faced a problem where I could not pass values to a servlet.

I tried to access the servlet directly by http://www.testing.com/jke/servlet/do.the.TestServlet

posting some values. I was not able to get them. I only got null. But after setting up

the mapping and accessing the servlet with .../jke/pattern it worked fine.

Hope I could help a little.

kelox_jd at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5
Thanks for the help guys, i'll post the final code, when i get to work!Cheers AgainCJ
CJCJ at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6

Below is the completed code, needs a few minor tweaks, but other than that done! GumB -thanks for the suggestion but i'd already figured it out from your first post!

EditLink - Produces a form that you change links on.

DoEditLink - Acts on the details you entered in the form.

Thanks Chris Moon

EditLink.java

/*********************************************************

* Edit code for Simple website data*

* By Chris Moon (C) April 2002 *

* May only be copied with the permission of the author *

* Author MUST be acknowledged in ALL COPIES produced*

* Regardless of Variable name changes*

*********************************************************/

import java.util.*;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class EditLink extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

String LinkContent;

int numberOf = 0;

//open the file

FileReader fr = new FileReader("ForLinksPage.Links"); //Change to correct file name

BufferedReader br = new BufferedReader(fr);

FileReader fr2 = new FileReader("ForLinksPage.Links"); //Change to correct file name

BufferedReader br2 = new BufferedReader(fr2);

res.setContentType("text/html");

PrintWriter out = res.getWriter();

out.println("<HTML>");

out.println("<script language=\"javascript\" type=\"text/javascript\" src=\"http://profprac2.sys.uea.ac.uk/Groupkk/New/top.htm\"></script>");

out.println("<H2><FONT FACE=\"Trebuchet MS\">Edit Links </FONT></H2>");

out.println("<CENTER><HR></CENTER>");

out.println("<FORM METHOD=GET ACTION=\"/groupkk/servlet/DoEditLink\">"); //Change to correct link!

out.println("<FONT FACE=\"ARIAL\">Please Alter a link then press the Edit or Delete button.<P> <B>EDIT or DELETE one link at a time.</B> Pressing a button returns you to this page!</B>"); //Change bits of this text

while ((LinkContent = br2.readLine()) !=null){

StringTokenizer numSt = new StringTokenizer(LinkContent,",");

String LinkDescription = numSt.nextToken();//Add more tokens if necessary

String Link = numSt.nextToken();

numberOf++;

}

out.println("<INPUT TYPE=HIDDEN NAME = NUMBER VALUE=\""+ numberOf + "\">"); //THIS LINE IS VERY IMPORTANT DO NOT TOUCH

int currentNumber=1;

numberOf++;

LinkContent = new String();

while ((LinkContent = br.readLine()) !=null){

StringTokenizer st = new StringTokenizer(LinkContent,",");

String LinkDescription = st.nextToken();

String Link = st.nextToken();

out.println("URL Description");

out.println("<INPUT TYPE=TEXT NAME=\"URLText"+currentNumber+"\" VALUE=\"" + LinkDescription + "\">"); //Change these as needed

out.println("URL"); //Change these as needed

out.println("<INPUT TYPE=TEXT NAME=\"URL"+currentNumber+"\" VALUE=\"" + Link + "\">"); //Change these as needed

out.println("<INPUT TYPE=SUBMIT NAME = \"EDIT"+currentNumber+"\" VALUE =\"Edit Link\">"); //Change these as needed

out.println("<INPUT TYPE=SUBMIT NAME = \"DELETE"+currentNumber+"\" VALUE =\"Delete Link\">"); //Change these as needed

out.println("<HR>"); //Change these as needed

currentNumber++;

}

out.println("</FONT></FORM>");

}

}

DoEditLink

/*********************************************************

* Edit code excuter for Simple website data*

* By Chris Moon (C) April 2002 *

* May only be copied with the permission of the author *

* Author MUST be acknowledged in ALL COPIES produced*

* Regardless of Variable name changes*

*********************************************************/

import java.util.Vector;

import java.io.*;

import java.lang.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class DoEditLink extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

res.setContentType("text/html");

PrintWriter out = res.getWriter();

String StrNumberOfURLS=req.getParameter("NUMBER");//NO Change

FileReader fr = new FileReader("ForLinksPage.Links");//Change to the correct filename

BufferedReader br = new BufferedReader(fr);

int currentNum=0;

int NumberOfURLS = Integer.parseInt( StrNumberOfURLS );

String URLString;

String LinkContent;

String EDITLINK = "Edit+Link";

String DELETELINK = "Delete+Link";

String editString = new String ("EDIT");

String deleteString = new String ("DELETE");

String URL="URL";

String URLText="URLText";

Vector URLS = new Vector();

while((LinkContent = br.readLine()) !=null){

URLS.addElement (LinkContent);

}

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

editString = "EDIT";

editString +=(String.valueOf((currentNum+1)));

String DOEDIT=req.getParameter(editString);

deleteString = "DELETE";

deleteString +=(String.valueOf((currentNum+1)));

String DODELETE=req.getParameter(deleteString);

if (DODELETE != null){

URLS.removeElementAt(currentNum);

DODELETE = null;

}

if (DOEDIT != null){

URL +=(String.valueOf((currentNum+1)));

URLText +=(String.valueOf((currentNum+1)));

String ActualURLText=req.getParameter(URLText);//Change to correct Passover name

String ActualURL=req.getParameter(URL);//Change to correct Passover name

URLString = ActualURLText;//Continue if necessary for more fields

URLString +=",";

URLString +=ActualURL;

URLS.setElementAt(URLString, currentNum);

DOEDIT = null;

}

currentNum++;

}

int vectorSize = URLS.size();

PrintWriter fileOut = new PrintWriter(new FileWriter("ForLinksPage.Links")); //Change to different file name

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

fileOut.println(URLS.elementAt(i));

}

fileOut.close();

out.println("><head>");

out.println("<title>HTML REDIRECT</title>");

out.println("<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0; URL=http://profprac2.sys.uea.ac.uk/groupkk/servlet/EditLink\">");

out.println("</head>");

out.close();

}

}

CJCJ at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 7

Looks like this is working now ...good job.

One suggestion though ...why not simply redirect instead of using the http header code.

I suppose it makes little difference, but I would have done this at the end of DoEditLink:

res.sendRedirect("../servlet/EditLink");

/**

out.println("<head>");

out.println("<title>HTML REDIRECT</title>");

out.println("<META HTTP-EQUIV=\"REFRESH\" CONTENT=\ URL=http://profprac2.sys.uea.ac.uk/groupkk/servlet/EditLink\">");

out.println("</head>");

out.close();

*/

GumB. at 2007-7-5 > top of java,Enterprise & Remote Computing,Web Tier APIs...