Wednesday, 18 September 2013

jsp elements in details


 JSP elements


Lets we talk about jsp elements now in details
we learnt that jsp is nothing but html with some java code
we know how to build html pages,
How do you put the java code in the html page? Answer is we can do that by using jsp elements
with the introduction of jsp elements, we can put java code in  your static html page, which will makes yours static pages, dynamic web pages
we can build dynamic web pages using jsp elements in your static html files which static html pages, jsp pages means dynamic pages

TYPES OF JSP ELEMENTS

1. JSP Declarations
2. JSP Scriptlets
3. JSP Expressions
4. JSP Directives

1. Why do we have 4 different jsp elements?
Ans:
 Whenever you want to put java in your html page we use jsp elements. Could not be nice to have only one jsp element? and whenever i use java in my html page again simple i put that jsp element in my html page.  Basically each one of JSP Element has its own significance.

JSP Declarations:

Whatever you put the code in JSP Declarations that code goes out side the service() method.
Where did the service() method come from? we know that service () is present in servlets then How did we get service() method in jsp? becoz jsp is nothing but a servlet, so whatever code you put in your jsp at the end it is going to be converted servlet. And whatever you put in JSP  , it goes outside the service() method so what does it mean? Let's say that you have some method you want to define that can be used by at many different places. You dont want to put that same logic at multiple places in your code so in turn you want to reuse your code, so you put that reusable code in that a different method from different places so how can you achieve that? you can achieve that using jsp declarations. So declare a method using jsp declarations which will put that method outside the service method so basically it is a class level method and alternatively you can even have certain varaibles define in outside the service() method which will be used by through out the class.

1. What are JSP Declarations?
Ans: Whatever goes inside the "<%!{java code here}%>" tags  is called a declaration. Everything you write in a declarations goes outside the service(). Treat them as instance methods and instance variables.

2. Advantages of using JSP Declarations?
Ans: variable declaration is you can reuse the value across the multiple places.

3. What do you declare in declarations?
Ans: you can declare methods and variables in declarations.

4. Why do you need JSP Declarations?
Ans: If you have a variable that needs to be shared across different requests. You have repeated common code in you jsp, declare with in a method.

Ex: <%! int instanceVar=10;%>

Here the same value of 10 is reflected  when someother requests comes as well and how is it possible, how does the value retained across multiple requests you know that ultimately a jsp converts into a servlet. So whenever a new request servlet comes in, the service() method is invoked only. And  the state of that particular class remains the same that particular object remains the same we know that servlet is a multithreading environment so whenever you make a new request a new thread is born and the service() is invoked. And whatever the outside the service() whatever instance variable in your class will be shared across multiple requests.
you have repeated common code in your jsp, declare it in a method.. Ex: <%! int instanceVar=10;%>, so lets see how really works.


Figure: jsp2.jpg


  • Here I have 3 requests, and lets assume that this page is one jsp page, and ultimately it got converted into a servlet, we know that servlets are multithreaded for every new request one new thread is called but the same servlet object.
  • So when request1 comes in it sponsores a new thread so basically I have a new service() method. When request2 comes in again I have new service() and ultimately when request 2 comes in, Im going to have a new service() method for request 3 as well. 
  • For each request it has it own service() method. So whatever is there within the service() method is not been shared across these three requests, different for each of the requests. Each request is unique now.
  • but when you come to declarations whatever you have in declarations is being shared by all these three requests . That is request 1 is going to share the same value, request 2 is going to share the same value, and request3 is going to shared the same value which you have in declarations.
  • So lets say that I have declared variable i and its value is 10 when its request 1 now when request 3 comes in tries to get the value of i , that is going to see the value of i that is 10 because declarations shared multiple requests.
  • Similarly if I would have put this value i=10 in service2() method, when i make the request 3 and I will not see the value 10 again that would be the default value probably zero.. Here if you declared any variables in side the service() methods that can not be shared multiple requests



JSP Scriptlets




  • Whatever code you put in Scriptlet it goes into the service() method so most of the time you would be writing scriptlet in your jsp pages because you know that when you are coding servlet at that time what you are doing is you are overriding the doGet() method or doPost() method which is similar to overriding the service() method.
  • Whatever you code write within these tags i.e: <%........> are treated as scriptlets.
  • In servlets most of the time you are overriding the doGet() or doPost() methods.
  • What do you do in the doGet() or doPost) method? you basically add your business logic to achieve the functionality that is to achieve the dynamic functionality. Similarly even in JSP whenever you want to add some java code most of the time you will be adding some business logic in your jsp pages and all your business logic is kept in the service() method because every new request, the service() method will be invoked and you want particular code to be executed whenever new request comes to the jsp page.


Explanations about the Scriptlets:

All the business logic which you want to put in java code in your jsp it could be in the scriptlets. We know that everything you write in scriptlets goes in the service(). Treat variables in scriptlets as method/local varialbes.
If I define any variables inside the scriptlets it could have gone in the service() so when I define variable i =10  in service2 , this variable i scope only in service2 only and it will not be visible in other service () methods  thus treat them as local variable or method level .

What do you put in scriptlets?
Ans:
Business logic in JSP are in scriptlets. Set of java statements.

Why do you need scriptlets?
Ans: 1. Need to perform some small business logic (if logic is complex, do in java). 2. Need to perform some basic validations.


So only reason is introduced java, in html was to produce dynamic pages content and when we say when we want to produce some dynamic content we need to perform some kind of validations, some kind of busines logic processing we can generate an output based on the the user input for that we need some kind of business logic that is the whole picture of scriptlets.

Example: <% int localVar=10;%>

JSP Expressions:


Basically whatever you put in JSP Expressions the code is evaluated and you get the value as a output so let's  say that I have a variable called int i=10 when I put variable i in JSP Expressions tags it is going to print 10 as a output but not variable i. So basically expressions whatver you put expressions in the expression tags and displays output value.

Explanation about the Expressions:

What are Expressions?
Ans: Whatever goes inside the "<%={java code here}>". Onething you have to note in jsp Expressions is whenever you put some in jsp expressions you do not put a semi colon( ;) at the end so that is not allowed.
Lets say if you want to print the value of variable i you can not say like this . <%=i;%>//invalid because of semicolon,
<%=i%>//ok
Code inside jsp expressions is evaluated, and output is displayed
Whatever you put inside expressions should evaluate to a value.
Basically sometimes you will need to print a certain texts on to the page, on to your web page, lets say the user logs in and after successfully login in on the login page or main page, we are going to display "Welcome username" , so while going to display this dynamic username you can not do it in static html because whatever you put in static html page will remain same for each and every user and you can not really change that value there so inorder to do that you need to insert dynamic content, whatever you enter the username we need to display that username in the main page so you can do that using scriptlets since you said that scriptlets whatever you code you put in scriptlets the expression is evaluated as a output  is displayed .

Example: <%=localVar %>
So eventhough we saw that how we can completely perform all the business logic processing and my java code within jsps it is not really recommended to do bulk of business logic of in your jsps because as we talk many times main role of jsp is it should be for presentation logic and not business logic. The role of the business logic is played by the servlets and its supporting classes and not by jsps . So jsp is only should use for presentation logic so inorder to present the jsp a webpage the user , inorder to present the dynamic web page to the user whatever dynamic certain amount of logic or certain amount of business logic we need to write in your jsp you can put that otherwise all the hard code business logic process should be done in servlets and other supporting java classes and we are going to simply call those methods from jsps .


JSP Directives

It is one of the important jsp element we have. JSP Directives basically commands to the JSP Engine. We will have complete details in future class.


Example for jsp elements:

<html><head><title>JSP Elements Example</title>
</head>
<body>
<%! int userCnt=0;%>  //declaration element

//scriptles
<%
    String name="Rajendra";
     userCnt++;
 %> //end of the scriptlet

<table>
 <tr><td>
  Welcome <%=name%> //here within <%=..%> is Expression element
     Your are user number<%=userCnt%>//Expression element
  </td></tr>
 </table>
</body>
</html>

/* output*/
Welcome Rajendra you are user number2

Explanation: here im the second person who has accessed web page it is going to display welcome Rajendra and you are user  number 2, lets say that some other guys comes in access this web page, is going to display: Welcome Rajendra you are user number 3. So for each request that comes it into this web page for everyone who login simple this web page and it is going to increase the userCont by one. By this time you would have triggered  at which place in this dynamic web page and it is use jsp elements and what type of jsp elements .

So we know that the variable userCnt is shared across multiple requests , in the same web page somebody else has to remember whats the previous count and it has to increase that count. So we know that using declaration elements we can share the variable across multiple requests . So im going to declare the userCnt variable  in the jsp declarations .
Next I have defined the value of Rajendra the variable called name, and this goes into the service() method, so for every new request this will be a new name, here simply I have defined Rajendra, but in real time, we need to fetch, whatever the user enter his username, that has to be displayed. And after each request increasing the userCnt by one.
And ultimately Iam goint to display the username and the userCnt. We know that we have to display value on to the screen we have to use JSP Expressions so I used jsp Expressions to display the name and the userCnt.


I hope you understood the jsp elements concepts.

Sunday, 8 September 2013

What is JSP ?

Introduction



  • jsp stands for java server pages.

before learning JSP , you should have understanding of HTML, JAVA AND SERVLETS

  • Because a lot of things we are going to talk in this session make sense, if you do not know any of these technologies.


Intoduction
JSP Elements
Implicit Objects
Tag Libraries
JSP Actions
Error Handling
Demo


  • In the servlet tutorials we have talked about lot of topics and all those concepts to apply in JSP as well , but not all most of the concepts do apply in jsp
  • JSP is used to presents dynamic content to users, so here again we are trying to say the same thing. Even in servlet tutorial , I told you that servlet is used to present dynamic content to the users.
  • And I told you that If want to present static content to the user, we use HTML
  • And when you want to dynamic content, you go for servlets, here again in JSP telling the same thing which is used to dynamic content to the users

-------------------------------------------------------------------------------------------------------------
So what is the difference between the servlets and jsps

Difference between Servlets and Jsp?


  • Basically both are used to present the dynamic content to the users. But their job is different.
  • Servlets are used for business processing logic where as jsps are used for presentation logic processing logic.


MVC Architecture overview:


And it is context am I talking all these about, Iam talking about context of these MVC Architecture
MVC is a very famous architecture, which stands for Model View Controller where each componant has its own job.

What is Model?

Here the model component is responsible to communicate with the data base. That is fetch the data or get the information from the database and make it available to the application to use

What is View componet?
The view component is responsible for presenting the page to the useror presenting the response to the end user and very beautiful and neat format. So we visit a lot of websites everyday, you see that the UI looks so good and all those credit goes to the view componant, so makes it so beautiful. And Jsp is used for view componet here and it is very strongly used view component.

What is Controller?
Next part is Controller, basically the controller controls the whole application flow, whenever a request comes into the application, the request is crapped by Controller, and the Controller is going to decide based on the request to which helper object should it make use of, which view object is going to use of ? and sends the response back to the user.
Basically the controller intercepts all the requests and it controls the whole flow of the application

Lets take a real time example of MVC Architecture

How a flow takes place in web application with servlets and jsps in place
When the user sends the request, the Web Container or Servlet Container is the interface between your, end user and the application
So the request goes to the Container, again Container will send to the request to the Servlet, so basically servlet is the place where you write the business logic.
And infact the Servlet will make use of certain other helper objects to completed business logic process
Lets say that this Helper Obect is talking to the Business Tier who executes certain statements (business Logic).
Gets back to the Servlet, the data is populated into some bean or some intermediate object within the application and the Controll is passed to the jsp
So the jsp is here the representing the presentation layer, and we put all the presentation logic within the jsp. So JSP is nothing but a html with some amount of java code in it. JSP acts as a presentation layer, we write html with dynamic code where add some java that's the reason we used dynamic content.
Once JSP does all its processing, and its sends back its response to the Web Container which Web Container will sends back to the end user
This is how Control flow in a web Application with both servlets and jsps.
Basically servlet role is taking the request and pop of the business logic processing and send the request to the jsp. Jsp role is take care of presentation logic and send back response to the user.

Servlets jsp
1. handles dynamic data 1. handles dynamic data
2. Handles business Logic 2. Handles presentation Logic
3. life cycles methods are 3. life cycles methods are
    init()    : can be overridden jspInit() : can be                           overridden
    service():  can be overridden _jspService(): can not                       be overridden
    destroy():  can be overridden jspDestroy(): can be overridden
4. html within java 4. java within html
   out.println("<html> <body=">);   <html><body>
   out.println("Time is="+new Date());   Time is<%=new Date()%>
   out.println("</body></html>");   </body></html>
5. Runs within a Web container 5. Runs within a web container


  1. Here both the servlets and jsp handles dynamic content. It has ability to taking the request, produce dynamic data based on request. 
  2. We saw that servlet handles business logic where as jsp handles the presentation logic part in an application. 
  3. We know that servlet life cycle methods are init(), service(), destroy() methods and we can override all these methods. Where as Life cycle methods of servlets are: jspInit(), _jspService(), and jspDestroy() methods , basically we can override jspInit(), jspDestroy() methods but we can not override _jspService() method. 
  4. Basically whatever methods starts with (_) underscore , that will not have ability to override and whatever the methods which we do not have underscores that can be overridden

So if we want to present the data to the user within servlets what can you do? we put html within java.

out.println("<html> <body=">);  
   out.println("Time is="+new Date());
   out.println("</body></html>");  
here we just write print and Time is  the current tim on to the page
simlarly in the jsp also we do the same thing, but the jsp is opposite i.e java within html

<html><body>
 Time is<%=new Date()%>
</body></html>


  • basically we are writing html code and whatever java code you need to write , in certain special tags example: Time is<%=new Date()%>. Where as in servlet we are writing html code within the java code
  • If we compare the both the code which do you think looks more pretty and easy to understand which is in jsp part rather than servlet.
  • In jsp , simply we have html code and certain special parameters where you put in your java code so for that reasons jsp is very popular for that reason we need a separate presentation layer to present the information to the end user. And we can not user in servlets to perform the presentation and as well as business logic
  • And both servlets and jsps are run within the web container.
---------------------------------------------------------------------------------------------------------

JSP is nothing but a Servlet


We already talked that in the end every jsp is nothing but a servlet so since we have same life cycle methods as well. Because whatever you write in the jsp in the end translated to the servlet
So whatever code you write in jsp either goes to the init() method , service ()method or destroy() method
So the programmer writes the jsp and it is translated to java file i.e MyJsp.jsp-->MyJsp_jsp.java and the java is ultimately translated to class file i.e MyJsp_jsp.class.
When you execute the jsp, when you call the jsp then it is loaded and initialized as a Servlet
So ultimately in the end jsp is nothing but a Servlet so all the features we have in servlet basically you will ultimately have jsp as well. You should have all the ability to use all the features in of servlet in jsp because a jsp is nothing but a servlet

Thursday, 5 September 2013

Where does the JSP code is placed in the Servlet?

Where does the JSP code is placed in the Servlet?




Where does the JSP code is placed in the Servlet?

Ans: I told you that a jsp is nothing but a servlet so how do you determine what elemlents goes into which part of the code in your servlet thats very important for you to know.
The first thing we have seen is Scriptlet: I told you that scriptlet is nothing but java code

<% int i=10; %>


And where did this go into your java code (servlet), so the above code goes to this part that is jspservice() in the following code:
public void _jspService(req,res)
{
 int i=0;
 out.println("<html>\r<body>");
 out.println("Hello Welcome");
}

jspService is nothing but an implementation of the service() method of the servlets
The Container will not override the doGet() or doPost() methods. Container simply overrides the jsp service() methods and puts all the business in that jsp service() method
So your scriptlets goes into the _jspService() method so whatever scriptlet I had written it has gone into the _jspService() method
Now Where did this declaration code will go?

<% int count=0;%> had gone the out side the service() method in servlets: int count=0; so whatever you put your declarations does not go into _jspService() method, only goes to outside the _jspService() method.
The declaration the following whole goes to outside the jsp service() method. The whole declaration which we had added in jsp will go to outside the service() method in servlet, which we can see clearly
<%!public void display()
{
 out.println("Hello");
}%>

  • Again we discussed about the page directives, here which i have imported foo.* has gone the import statement in the Servlet class i.e: import foo.*;
  • Ultimate final Servlet class. Now you apart from these jsp element scriptlets, desclaratons, ... and I also have the html that is my general html so where does this html code goes 
  • In the above jsp code we observed that "Hello Welcome" is nothing but a html code so all the html code of the jsp code goes to the _jspService() method


public void _jspService(req,res)
{
 int i=0; //scriptlet
//html code comes here
 out.println("<html>\r<body>");
 out.println("Hello Welcome");
 out.println("</html>\r</body>;
}


  • So jsp code has been converted into servlet code by the Web Container
  • So here you can see the Container converted the out.println() statement

jsp elements

jsp elements


jsp elements


  • Now let us discuss some of the jsp elements. I told you that you can write java code within jsp elements, jsp tags. So apart from that general html tags whatever you have you can even embeded some java elements.
  • How do you embeded that java elements? using these jsp elements.

The following are the 4 jsp elements:

1.scriptlets
2. Expressions
3. Declarations
4. Directives

So these are four different jsp elements, which You can embed jsp file apart from the html tags . So number 1. Scriptlets

So whatever java code you want to write within your jsp, you can put that in just scriptlet tag.
Example:
                <% int i=10; %>
                <%Dog d=new Dog();%>

  • when you say angular brackets percentile will be treated as a pure java code so whatever java code you want to write in your jsp's you can put them within scriptlet tags


2. Expressions:

<%=i%>
<%=d.getName()%>


  • Expressions looks something like this: angular brackets, persentile equal to some java code but what happens this values of expressions.
  • Whatever you put this within the expression tag goes directly to the output this "out.println() statement.

Ex:
<%=i%>                    is  goes to directly like this out.println(i)
<%=d.getName()%>  is  goes to directly like this out.println(d.getName())

Here out.println() means you are printing something to the UI. So basically it prints out this value of i and so on
Basically Expressions elements are input to out.println() statements

3. Declarations

Ex:
<%! int i=10;%>
<%! void display() { System.out.println("Hello");}%>

declarations again something like this angular brackets, percentile and explanation mark , again you write java code in that .
But whatever you want to perform initialization that has to be done only once you put that in the declarations elements

4. Directives

Ex:
Pages <%@page import="foo.*,bar.*"%>
include<%@include file="/foo/myJsp.jsp"%>
taglib<%@taglib url=Tags" prefix="cool"%>

We have different types of directives, those are :

pages,
include,
taglib


  • Basically when a jsp is translated and convreted back into your servlet.
  • Whats the first thing the Web Container does? The first thing the container does is it looks for the directives elements, that is it looks for all the directives. That is directive elements starts with : angular brackets percentile at the rate so it looks for all the directives and it checks whats the directive is ? and acts accordingly and it process the directive and then it goes in to the code and tries to converts all these that is whatever java scriptlets, expressions and declarations we write. It tries to converts thats into servlets. But the first things its look into and converts the directives ok
  • So here we have "page" directive, in the page directive we can write "import" statements  like general java imports whatever the classes you want to import, packages classes 
  • And the include directive, you can include files so whatever file you inlcude here will directly be replaced in your jsp files so for example, in myJsp, i say "include<%@include file="/foo/myJsp.jsp"%>"  so whereever i write this code in a.jsp the whole content of b.jsp is replaced as it is in my a.jsp at the location where I write this include statements


taglib :

  • you have this uri and prefix attribute for this taglib directive
  • So basically you can add taglibrary to your jsp and thats a little bit advanced concepts but right now we are not discussing in this session. taglibrary directive will be discussed in JSP class



  • So basically these are the 4 jsp elements you can put in a jsp. So you can have only scriptles, expressions, declarations, directives apart from the basic html tags which you have in your jsp's
Where does the JSP code is placed in the Servlet?

Ans: I told you that a jsp is nothing but a servlet so how do you determine what elemlents goes into which part of the code in your servlet thats very important for you to know.
The first thing we have seen is Scriptlet: I told you that scriptlet is nothing but java code

<% int i=10; %>



  • And where did this go into your java code (servlet), so the above code goes to this part that is jspservice() in the following code:

public void _jspService(req,res)
{
 int i=0;
 out.println("<html>\r<body>");
 out.println("Hello Welcome");
}


  • jspService is nothing but an implementation of the service() method of the servlets
  • The Container will not override the doGet() or doPost() methods. Container simply overrides the jsp service() methods and puts all the business in that jsp service() method
  • So your scriptlets goes into the _jspService() method so whatever scriptlet I had written it has gone into the _jspService() method

Now Where did this declaration code will go?

<% int count=0;%> had gone the out side the service() method in servlets: int count=0; so whatever you put your declarations does not go into _jspService() method, only goes to outside the _jspService() method.
The declaration the following whole goes to outside the jsp service() method. The whole declaration which we had added in jsp will go to outside the service() method in servlet, which we can see clearly

<%!public void display()
{
 out.println("Hello");
}%>


  • Again we discussed about the page directives, here which i have imported foo.* has gone the import statement in the Servlet class i.e: import foo.*;
  • Ultimate final Servlet class. Now you apart from these jsp element scriptlets, desclaratons, ... and I also have the html that is my general html so where does this html code goes 
  • In the above jsp code we objserved that "Hello Welcome" is nothing but a html code so all the html code of the jsp code goes to the _jspService() method


public void _jspService(req,res)
{
 int i=0; //scriptlet
//html code comes here
 out.println("<html>\r<body>");
 out.println("Hello Welcome");
 out.println("</html>\r</body>;
}


  • So jsp code has been converted into servlet code by the Web Container
  • So here you can see the Container converted the out.println() statement


Overview of JSP

servlets: HTML within java business logic

public void doGet(request, response)
{
 PrintWriter out=response.getWriter();
 String name=request.getParameter(name);

 out.println("<html><body>");
 out.println("Hello"+name);
 out.println("</body></html>");
}

JSPs: java within HTML Presentation Logic
<html>
 <body>
  <% String name=request.getParameter(name);%>
  Hello<%=name%>
 </body>
</html>

Initially web server or HTTP server can handle and service only static http pages but where as if you want to go to any dynamic content you have to go with servlets
So how do you basically print out a dynamically content on a html? This is the answer to that, you write out.println() statement on your servlet (i.e out.println("<html><body>")
In client URL the following code will be visible:

out.println("<html><body>");
 out.println("Hello"+name);
 out.println("</body></html>");
So out.println() goes to the client screen, so whatever it says, actually it is a output. Which is sent it as a response to the client
This is nothing but here what I'm doing Just writing simple do..get method Im getting response.Writer() and simply out.println("<html><body>");
And println("hello"); i.e  out.println("Hello"+name);
That means this is the html i.e <html><body> file and it only one word that is "hello" and "name" of the person in the body of that file
So this is how servlets is able to putting the UI code in its class
But you have a look at this is a mixture of business logic with html code
Basically servlets use only to perform some business logic for creating dynamic content.
But here what you are doing is you are perfoming business logic in the dynamic content but at the same time you are even doing html code in servlet itself so this is very bad practice
You have actually your apps in such a way that your business logic entity is seperate and your presentation logic entity is separate .
You have to seperate these things . Business Logic and Presentation Logic are two separat things but it should not be makes in only one thing which creates more confusion and its not very clear
So your servlet only take care of only business logic part and the presentation logic part should be done somewhere else.
What is that somewhere else? Where is that some where else?
Here somewhere else is nothing but JSP's. This is where JSPs had come into the whole servlet picture

JSPs: java within HTML Presentation Logic
<html>
 <body>
  <% String name=request.getParameter(name);%>
  Hello<%=name%>
 </body>
</html>
JSPs are used to provide the presentation logic to the client so this is nothing but JSP s are similar to HTMLs
But within this some special kind of HTML, you can write java code within this HTML
So HTML+JAVA =JSPs
So JSPs take care of all of your presentation logic for beautiful UI look and your servlet actually should take care of your business logic. So separation of concerns is what we are doing here
JSPs are only concerns only presentation logic and servlets should only concens only Business logic




Now but in the end JSP is nothing but a servlet everything you write a jsp ultimately goes into a servlet so who creates this servlet, it is the Web Container whose responsible  to create a servlet out of this JSPs.
So as we discussed JSP is nothing but a HTML + Java, ultimately this whole thing is converted into a servlet so previosly we have seen that when you write the out.println()

public void doGet(request, response)
{
 PrintWriter out=response.getWriter();
 String name=request.getParameter(name);

 out.println("<html><body>");
 out.println("Hello"+name);
 out.println("</body></html>");
}
ultimately when you write a jsp program nice UI look and html tags and all those stuff like below:

JSPs: java within HTML Presentation Logic
<html>
 <body>
  <% String name=request.getParameter(name);%>
  Hello<%=name%>
 </body>
</html>
And the Web Container converts the jsp program into a servlet (like below)

servlets: HTML within java business logic

public void doGet(request, response)
{
 PrintWriter out=response.getWriter();
 String name=request.getParameter(name);

 out.println("<html><body>");
 out.println("Hello"+name);
 out.println("</body></html>");
}

And your servlet looks the same as out.println() statements. But this is not your responsiblity. Conversion of jsp to servlet is the responsible of web container.
So you basically write as a programmer in jsp page where you will put your html tags all the java content in it and that servlet is translated into a java file (Ex: MyJsp_jsp.java) and then java file is compiled and it is loaded and initialized as a Servlet
So ultimately in the end a jsp is nothing but a servlet.
So when the first request for jsp comes in what container does is it translates a jsp into a java, compiles it and loades that compiled java class as a servlet from there on it is a servlet so basically a jsp is also a servlet but when you code in as a jsp then it ultimately becomes a servlet.

Thursday, 12 July 2012

Introduction


Java Server Pages(JSP) is a technology from Sun that enables the Java devolopers to generate HTML, XML or other types of documents to server the web client. The JSP technology allows the programmers to embed Java code into html (.jsp) page.

Java Server Pages are first compiled into Java Servlets by a JSP compiler and then this Servlet is loaded by the Servlet container to server the client request.

JSP is grate technology to simplify the development of web-based application using Java technology.

Wednesday, 4 April 2012

First JSP PROGRAM (COMPARING WITH SERVLET PROGRAM)

STEPS:
1. CREATE THE FOLDER (Ex: Introduction)
2. WRITE HTML PROGRAM (EX: index.html)
3. write the servlet program (Ex: FirstServlet.java)
4. set the classpath for Tomcat Server
5. compile the servlet program to get class files
6. create WEB-INF folder
7. under WEB-INF folder
8. write web.xml file
9. create classes folder
10. create lib folder
11. prepare the WAR file (optional)
12. deploy the application(war file) into the server
13 Run the server
14. execute in the browser
//index.html
<html>
 <head>
 <title>time and date</title>
 </head>
 <body>
 <form action="ExServlet" method="get">
 <input type="submit" value="click here"/>
 </form>
 </body>
</html>


//FirstServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;
public class FirstServlet extends HttpServlet
{
 public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
 response.setContentType("text/html");
 PrintWriter out=response.getWriter();
 out.write("<html>");
 out.write("<head>");
 out.write("<title>First jsp page</title>");
 out.write("</head>");
 out.write("<body>");
 out.write(new Date().toString());
 out.write("<br/>Hello World! <br/>");
 out.write("</body>");
 out.write("</html>");
 out.close();
}
}
/*

D:\webtechnologies\jsp\prog\Introduction\fistServletApp>javac FirstServlet.java

D:\webtechnologies\jsp\prog\Introduction\fistServletApp>
*/

web.xml

<web-app>
<servlet>
  <servlet-name>serv</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>serv</servlet-name>
<url-pattern>/ExServlet</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

Now we will do the same program in JSP

Steps:

1. create any folder (Ex: firstJsp)
2. write the jsp program
3. deploy the folder  (firstJsp)
4. start server
5. deploy our application
6. run the jsp program


2. write the jsp program
<%@ page import="java.util.Date;"%>
<html>
<head>
<title>First JSP Page</title>
</head>  
<body>
<%
out.println(new Date().toString());
%>
<br>Hello World! </br>
</body>
</html>

3. deploy the folder  (firstJsp) into webapps folder of Tomcat server
4. start the server
5. go to any browser, 
6. type http://localhost:8888/
click on Tomcat Manager

7. type username and password
8.  select firstJspApp folder from the list
9. type firstpage.jsp at the end of the url ie. http://localhost:8888/firstJspApp/firstpage.jsp