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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 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…

726 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