login authentication using jsp pages

hi,

i have a page called 'main' to acess the 'update' page you must log in to view this page. but once your logged in i dont want it to take you to the login page if you require to go back

is there a way of checking if the user is logged on prior to chaning the page? when a link is checked so that i may check a session variable?

at the moment the 'update page'

reads in the session value but i dont know how to say if its null open a different page?

<%

String username = request.getParameter("EmployeeName" );

if (username ==null)

{

System.out.println("hello");

}

session.setAttribute("userName", username );

Message was edited by:

h1400046

[942 byte] By [h1400046a] at [2007-11-26 15:03:11]
# 1

To open a different page you can use the

<jsp:forward tag, the detailed syntax for this tag is available here:

http://java.sun.com/products/jsp/syntax/2.0/syntaxref2018.html#1003349

Another better option is to use JSP Tag Libraries (JSTL) and use the

><c:redirect tag , here's the full syntax:

http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html>

appy77a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

thanks..

so within the body ive placed:

<c:if username= "${<%= session.getAttribute( "username" ) %> == 'null'}" >

<c:redirect url="http://localhost:8080/Experience/login.jsp":/>

</c:if>

i know that 'username' is defently null because im printing it to the screen?

the error is

org.apache.jasper.JasperException: /update.jsp(29,50) equal symbol expected

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

org.apache.jasper.JasperException: /update.jsp(29,50) equal symbol expected

org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)

org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:405)

org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:86)

org.apache.jasper.compiler.Parser.parseAttribute(Parser.java:193)

org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:143)

org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1338)

org.apache.jasper.compiler.Parser.parseElements(Parser.java:1577)

org.apache.jasper.compiler.Parser.parse(Parser.java:126)

org.apache.jasper.compiler.ParserController.doParse(ParserController.java:211)

org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)

org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:155)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)

org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

h1400046a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

A couple things in the new code you've provided.

You can't mix JSP Expressions, inside Expression Language (EL) the code that is inside ${}

So instead of

<c:if username= "${<%= session.getAttribute( "username" ) %> == 'null'}" >

it needs to be like this

<c:if test="${!empty sessionScope.username and sessionScope.username == username" >

And instead of

<c:redirect url="http://localhost:8080/Experience/login.jsp":/>

it is better to specify a context relative path like this

<c:redirect url="/Experience/login.jsp":/>

If you need more info on JSP Tag Libraries syntax and Expression Language syntax, the specification documents have them written very well.

Over here: http://jcp.org/aboutJava/communityprocess/final/jsr052/index2.html

Click Download.

Then get the jstl-1_1-mr2-spec.pdf document

Let us know if the above modified JSTL code works.

appy77a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

it throws the following exception:

org.apache.jasper.JasperException: /update.jsp(35,41) The attribute prefix does not correspond to any imported tag library

org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

ive imported:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

do i need more?

h1400046a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Did you place jstl.jar (from JSTL1.1) under WEB-INF/lib/ folder of your project?

Also what version of Servlets are you using. It is better to use the latest Servlet 2.4. If you are using servlet 2.4 you need to update web.xml to reflect Servlet2.4 version , your web-app tag should look like this

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

appy77a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

>Did you place jstl.jar (from JSTL1.1) under WEB-INF/lib/ folder of your >project?

yes

>Also what version of Servlets are you using. It is better to use the latest >Servlet 2.4. If you are using servlet 2.4 you need to update web.xml to >reflect Servlet2.4 version , your web-app tag should look like this

><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" >xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee >http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

i dont believe i have installed servlets at all? i am using tomcat, how would i find out?

h1400046a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Tomcat comes with the servlet JAR file by default. You don't need to install it separately.

Look under

\apache-tomcat-5.5.12\common\lib\

There should be a servlet-api.jar , or something similar.

I am assuming that you are using the latest stable release of Tomcat, that is version 5.5

appy77a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
thanks... yes thats right i have that file and i am using 5.5 to use that what do i need to import in my jsp?
h1400046a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

In addition to jstl.jar, your JSTL downlad should have come with standard.jar . Did you also place the standard.jar under /WEB-INF/lib/ ?

And change

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> (for JSTL 1.0)

to

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> if you are using JSTL1.1

Message was edited by:

appy77

appy77a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
yep, i have got the standard.jarthe jstl import doesnt help, but it also breaks other working jstl code so maybe i dont have 1.1?
h1400046a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
well i defently have jstl 1.1 , but that import still breakes everything!do i need to import any of the tlds?
h1400046a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

The TLDs are already present in the JSTL jar files, if you are using JSTL1.1. For older versions of JSTL it was required to specify the TLDs in web.xml. But for the new version, you will just need the JAR files, no need to add TLDs to web.xml

Sometimes it helps to leave everything aside and start from scratch.

Carefully go through the installation instructions, first download and install Tomcat5.5 so that you have the latest.

Then go through the installation instructions for JSTL1.1

Use the latest JSTL code, (not from JSTL1.0.) If you look at the new JSTL 1.1 API docs, it will have the latest syntax. (Avoid copying JSTL code from old online tutorials and old books, as the syntax has changed)

Then if it still doesn't work, you can upload the WAR file to some location and we will examine the WAR file to see if there's anything else that's wrong.

Message was edited by:

appy77

appy77a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
hiok ive re-installed tomcat, thought id get the most reacent so went for 6 and then installed jstl as it says, put the jars and the import and it does exactly the samei have no idea how to create a war file...
h1400046a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14
You can ZIP the project directory for now and upload it.But for later, you can use Ant script to create WAR files. Setting up Ant scripts takes a while since you'll first need to learn how to use Ant. It's easy but takes time. You can do that later.
appy77a at 2007-7-8 8:52:37 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15
where shall i upload it to?
h1400046a at 2007-7-8 8:52:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16
I don't know. Yahoo used to have a shareable briefcase, but I doubt if they have it anymore.
appy77a at 2007-7-8 8:52:38 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...