Seeting http headers in request in JSP page

A

Arun Goel

HI,

How can I send a http header from a jsp page to servlet?

I have a FORM in jsp page, when user enter the form & submit it , I
need to send some header to the servlet, how can I send it to the
servlet from jsp page?

I am pasting my code , (it is not working)..
--------------------------------------------------------------------------------
<html>
<head>
<title> Trying to access E-vista </title>
</head>

<body bgcolor="12a145">
<h1> Welcome to alternate way to enter artemis </h1>
<form name="artemis" method="post"
action="http://10.161.64.179:8085/Goel/servlet/PPage">

<center>
UserName : <input type="text" name="user" />
<br /><br />
Password : <input type="password" name="password" />
<br /><br /><br /><br />
<%
String userName = request.getParameter( "user" );
String password = request.getParameter( "password" );
response.setHeader( "Authorization", "BASIC " + userName + ":" +
password );
%>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</center>
</form>
</body>

</html>
 
T

Tony Morris

Arun Goel said:
HI,

How can I send a http header from a jsp page to servlet?

I have a FORM in jsp page, when user enter the form & submit it , I
need to send some header to the servlet, how can I send it to the
servlet from jsp page?

I am pasting my code , (it is not working)..
-------------------------------------------------------------------------- ------
<html>
<head>
<title> Trying to access E-vista </title>
</head>

<body bgcolor="12a145">
<h1> Welcome to alternate way to enter artemis </h1>
<form name="artemis" method="post"
action="http://10.161.64.179:8085/Goel/servlet/PPage">

<center>
UserName : <input type="text" name="user" />
<br /><br />
Password : <input type="password" name="password" />
<br /><br /><br /><br />
<%
String userName = request.getParameter( "user" );
String password = request.getParameter( "password" );
response.setHeader( "Authorization", "BASIC " + userName + ":" +
password );
%>
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</center>
</form>
</body>

</html>
-------------------------------------------------------------------------- ------
Thanks
Arun

You are sending a HTTP *response* to the client, hence the client picks up
the header.
A JSP doesn't send HTTP headers, the HTTP client does, and there is no way
(that I know of) to write HTML that sends request headers to the server.
The JSP executes as a servlet on the server side.

What you are trying to achieve looks like Basic Authentication.
Why are you "rolling your own" security ? This is prone to major problems.
The Servlet 2.x Specification has mechanisms for enabling Basic
Authentication on resources, such as JSPs.
I suggest you read up on those mechanisms.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
W

William Brogden

Arun Goel said:
HI,

How can I send a http header from a jsp page to servlet?

I have a FORM in jsp page, when user enter the form & submit it , I
need to send some header to the servlet, how can I send it to the
servlet from jsp page?

I don't think you can - however you can add lots of information to
the request by writing hidden input fields into your form.
 
A

Arun Goel

Tony Morris said:
You are sending a HTTP *response* to the client, hence the client picks up
the header.
A JSP doesn't send HTTP headers, the HTTP client does, and there is no way
(that I know of) to write HTML that sends request headers to the server.
The JSP executes as a servlet on the server side.

What you are trying to achieve looks like Basic Authentication.
Why are you "rolling your own" security ? This is prone to major problems.
The Servlet 2.x Specification has mechanisms for enabling Basic
Authentication on resources, such as JSPs.
I suggest you read up on those mechanisms.
==Thanks a lot for your help. I wasn't sure in first place if I can
send http headers or not. Actaully there is third party servlet ,
which prompts for username/password(I guess using WWW-Authentication
in respose header) , I was just trying to see , if I can send the
"Authorization" header while invoking the servlet, then servlet won't
prompt for password.

Actually learning about JSP & servlets & trying to play with stuff to
learn more..

Thanks a lot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top