secure my web page

hi friends,

am working on jsp with apache tomcat as web server...

i have a login page admin.jsp ...in that page i have button called

create.. if i click this button it directs me to a web page createusers.jsp...this page is for creting users...

but the problem isss if u place this createusers.jsp in url box still u can access this page....

i don want to happen like this ... i want createusers.jsp to open only when i click that buttoncreate in admin.jsp page...

hope some body helps me out in solvin...

thanks in advance

Ganesh

[603 byte] By [gania] at [2007-10-3 0:13:51]
# 1
try to learn session management
jgalacambraa at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
hi, thanks for ur reply..... can u help me with a sample code....... thanks in advance
gania at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

[nobr]try the following.. its up to you if you want to use it:

index.jsp

<%@ page contentType="text/html;charset=windows-1252"%>

<%

String error = request.getParameter("error")!=null?request.getParameter("error"):"";

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>untitled</title>

</head>

<body>

<form action="success.jsp" method="POST">

<table cellspacing="0" cellpadding="2" border="0" width="400">

<%if(error.equals("1")){%>

<tr>

<td colspan="2">

<font color="red"> You are not logged. Loggin First</font>

</td>

</tr>

<%}%>

<tr>

<td>Username</td>

<td>

<input type="text" name="username"/>

</td>

</tr>

<tr>

<td>Password</td>

<td><input type="password" name="password"/></td>

</tr>

<tr>

<td colspan="2">

<input type="submit" value="Submit"/>

</td>

</tr>

<tr>

<td colspan="2">

<a href="mainpage.jsp">To Main Page</a>

</td>

</tr>

</table>

</form>

</body>

</html>

success.jsp

<%@ page contentType="text/html;charset=windows-1252"%>

<%

String username = request.getParameter("username")!=null?request.getParameter("username"):"";

String password = request.getParameter("password")!=null?request.getParameter("password"):"";

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>untitled</title>

</head>

<body>

<%if(username.equals("gani") && password.equals("gani")){

session.setAttribute("username",username);

%>

<a href="mainpage.jsp">Main Page</a>

<%}else{%>

<font color="red">Sorry invalid login!</font><br/>

<a href="index.jsp">Login</a>

<%}%>

</body>

</html>

mainpage.jsp

<%@ page contentType="text/html;charset=windows-1252"%>

<%

String username = session.getAttribute("username")!=null?(String)session.getAttribute("username"):"";

if(username.equals("")){

response.sendRedirect("index.jsp?error=1");

}

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>untitled</title>

</head>

<body>

<h2>Welcome <%=username%>!</h2>

<a href="logout.jsp">Logout</a>

</body>

</html>

logout.jsp:

<%@ page contentType="text/html;charset=windows-1252"%>

<%

session.invalidate();

response.sendRedirect("index.jsp");

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>untitled</title>

</head>

<body>

</body>

</html>

username is gani and password is gani also.. hardcoded..heheh[/nobr]

jgalacambraa at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
thank u very much...........it is working....... can u tell me the concept behind this .... i mean a bit of session management... thanks in advance
gania at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

as you can see in the success.jsp this line:

session.setAttribute("username",username);

sets the value of username to a session variable also name username.. now clicking the link "main page" will go to mainpage.jsp.. in the mainpage.jsp, there is some checking username if it is "".. i put the:

String username = session.getAttribute("username")!=null?(String)session.getAttribute("username"):"";

os that it varible username will still have the value of "" if it is null to avoid exceptions.. now if the value username is "" it will then be redirected to index.jsp which is the login page.. the link on the mainpage.jsp (logout) will be the one that sets the session variables to null using the session.invalidate()

method.. so if you click logout on the mainpage.jsp, it will redirect to index.jsp and the value of the session variable named username will become null.. hope you understand my english :)

jgalacambraa at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

excuse me.........

there is a change

hi friends,

am working on jsp with apache tomcat as web server...

i have a login page admin.jsp ...in that page i have button called

create.. if i click this button it directs me to a web page createusers.jsp in a new window ..

but the problem isss if u place this createusers.jsp in url box still u can access this page....

i don want to happen like this ... i want createusers.jsp to open only when i click that button create in admin.jsp page...

i don want to use any login forms here... i mean page asking for user name & password because i already have admin.jsp for

hope some body helps me out in solvin...

thanks in advance

Ganesh

gania at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
excuse me jgalacambra ... can u help me out.....
gania at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
you cannot acheive something if you don't start at the basic
jgalacambraa at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
double post http://forum.java.sun.com/thread.jspa?threadID=751235&tstart=0
shuinia at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
guys ...... atlassss i solved....got the solution................ thank u friends for helping out
gania at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
i specially thankjgalacambra .. hope we continue the same in future...
gania at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
how about posting your solution so that others may be able to learn from your work ?Just a thought
Aknibbsa at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
did you use j_security_check?
jgalacambraa at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14
hi......... ya sure definately will do that.......
gania at 2007-7-14 17:04:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15

<%

String username = session.getAttribute("Empname")!=null?(String)session.getAttribute("Empname"):"";

if(username.equals(""))

{

response.sendRedirect("/proj/frame.jsp");

}

%>

when ever u retrive session values in ur web pages make sure the attribute name is the same.....

i secured my web page in this way ... thank u every body

gania at 2007-7-21 9:01:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...