Go Premium for a chance to win a PS4. Enter to Win


Problem with httpservletresponse.setHeader("referer", blah)

Posted on 2004-03-31
Medium Priority
Last Modified: 2010-05-18

Not sure if what i'm trying to do is even possible but i'll try to explain as best i can...

I'm writing a simple web-app using servlets. I want to use only one servlet to deal with everything.

At the moment, when a request comes in, the servlet does a request.getheader("referer") and depending on the value, executes a relevant if clause. The code in these if statements dynamically generate some html to be returned to the client.

The problem I'm having is this. I have an html page called "selectdb.html". When you click the submit button on this page, the servlet recognises the header as mentioned above and performs an SQL query on a database. The results of this query are filtered and returned to the client as a dynamically generated html page with a drop down menu detailing the tables in the db and a submit button. Now, when you click the submit button on this page, the referer in the header of the request gets set to the name of my servlet (which is to be expected seeing as it just generated it). However, I want to be able to set the header to something else so that when the submit button on the dynamically generated page is clicked, it can trigger another if statement on the servlet. I have tried response.setHeader("referer", "blah") and response.addHeader("referer", "blah") but to no avail. The header of the request sent from the dynamically generated page is always set to the name of the servlet no matter what.

Here is the code in question...

(temp is the last part of the url of the referer)

if (temp.equals("selectdb.html"))
    response.setHeader("Referer", "");
    out.println("<form action=\"\"method=\"post\">");
    String result="";
    try {db = new DBConnection();} catch (Exception e) {out.println(e);}
    try {result = db.executeSQLQuery("SHOW TABLES");} catch (Exception e) {out.println(e);}
    String[] tokens = result.split("<td>");
    out.println("<select name=\"tables\">");
    for (int i=1;i<tokens.length;i++)
        tok = new StringTokenizer(tokens[i], "<");
    out.println("<input value=\"Submit\" type=\"submit\">");

I want
if (temp.equals(simon.html)) to match when you click on the submit button of the html page generated by the above

I'm not sure if what i'm asking is possible but I hope someone can shed some light on the issue. Thanks, Simon
Question by:arytheman
  • 3
LVL 86

Expert Comment

ID: 10723396
Well you don't need it to be referer - you could make it an arbitrary, custom header, but a better way would be just to write a hidden input field and set the value according to the logic
LVL 86

Accepted Solution

CEHJ earned 375 total points
ID: 10723412
out.println("<input type=\"hidden\" name=\"secret\" value=\"doMyDbStuff\" />");

Author Comment

ID: 10723496
Nice one, didn't think of doing it that way. Thanks
LVL 86

Expert Comment

ID: 10723508

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question