Writing your first JSP  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews JSP
Written by MS   
Monday, 16 April 2007

{mos_sb_discuss:34}

You should have the basic knowledge of Core Java.

JSPs are used to generate dynamic WebPages by including the java code inside the html. The JSP file are stored as .jsp and they need to be run on a server you can’t just open a JSP file in a browser like you do incase of .HTML files. So before you can run your jsp file you need to download and install a server (You can try downloading Jboss or tomcat- www.Jboss.org ).


Incase you download Jboss you need to set the SYSTEM variable JBOSS_DIST pointing to JBOSS folder. Also make sure you have the latest JRE(java runtime environment) installed. Now go to the JBOSS_DIST/bin and start the server by running run.bat file. Now try to open http://localhost:8080 , if you see the welcome page then you are ready to run your JSP files.

You need to create and deploy a .war file on the server. For JSP applications you need to have a specific directory structure as:

First_JSPproject→ JSP files
→ WEB-INF → classes → java files
→ web.xml


You need to create the above directory structure and place and place the files in their corresponding places. Web.xml is a must even if it is empty. Create above directory structure and create the web.xml file as follows:



PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">


120



You do not worry about the web.xml we are not going to use it here, so for the time being just copy and paste.

Now we are ready to write and run our first jsp file. Create a file inside First_JSPproject named hello.jsp as follows:

Hello


Congrats you have run your first JSP





Now we need to create the war file and deploy it on the server. If you are using tomcat 5.0 or latest you just copy and paste the First_JSPproject into the webapps folder and open http://localhost:8080/First_JSPproject/hello.jsp. For creating the war file go to the First_JSPproject folder though command prompt and run the following command:

Jar.exe –cvf warfilename.war *.jsp WEB-INF

Make sure that your have the JAVA_HOME in the classpath

Now place this war file inside the JBOSS_DISTserverdefaultdeploy folder and restart the server.

Open http://localhost:8080/warfilename/hello.jsp

The browser will display “Congrats you have run your first JSP” you must have noted that we have not put any dynamic content here, so let us just do that.

Create a file named dynamicJSP.jsp

<%@ page import=”java.util.*” %>
Hello


The current time is <%= new java.util.Date()%>





<%@ directive attribute=”value” %> This is the syntax for a directive. More about it later. The first line is just similar to import java.util.*; in core java.
<%= “whatever” %> This is the syntax for a expressions in JSP, what ever you put inside <%= and %> will be converted to string and displayed. So here we are converting the date into string and displaying it. So the out put should be

The current time is SYSTEM DATE.

Create the war file and deploy it you should see the output same as above.


User reviews

Average user rating from: 1 user(s)

Overall rating
5.0
 

Add new review



5 of 6 people found the following review helpful

Excellent, Friday, 20 April 2007

Written by Deepak Agarwal

Overall rating
5.0
This dude is excellent. It was a night mare to me to learn the jsp.. but it really help me... keep it up...!
Was this review helpful to you? yes     no


Powered by jReviews

Last Updated ( Wednesday, 06 June 2007 )
 
< Prev   Next >