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

Posted on 2004-03-31
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 125 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 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