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


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
Who is Participating?

Improve company productivity with a Business Account.Sign Up

CEHJConnect With a Mentor Commented:
out.println("<input type=\"hidden\" name=\"secret\" value=\"doMyDbStuff\" />");
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
arythemanAuthor Commented:
Nice one, didn't think of doing it that way. Thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.