Information About JSP ,Servlets,Struts

54
INFORMATION ABOUT JSP , SERVLETS & STRUTS Made by: ALKESH KHARE

description

Advance Java Programming of two number addition using JSP (Java Server Pages) only and servlet only in Netbeans.Using Struts framework for insertion in a Database Microsoft SQL server 2005 from JSP webpage.

Transcript of Information About JSP ,Servlets,Struts

INFORMATION ABOUT JSP ,SERVLETS & STRUTS

Made by:ALKESH KHARE

Contents1 Introduction to JSP

1.1 Advantages 1.2 Disadvantages 1.3 JSP Example 1.3.1 Program for addition in JSP

1.3.2 JSP Program using Request Dispatcher2 Introduction to Servlet

2.1 Advantages2.2 Disadvantages2.3 Servlet Example2.3.1 Program for addition in Servlet

3 JSP & Servlet works in MVC Format4 Introduction to Struts

4.1 Advantages4.2 Disadvantages4.3 Struts Example4.3.1 Program for inserting data in Database

JavaServer Pages

JavaServer Pages (JSP) is a technology that helps software developers to create dynamically generated web pages based on HTML, XML, or other document types. Released in 1999 by Sun Microsystems, JSP is similar to PHP, but it uses the Java programming language.

Advantages of JSP :Following is the list of other advantages of using JSP over other

technologies vs. Active Server Pages (ASP): The advantages of JSP are

twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.

vs. Pure Servlets: It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML.

vs. Server-Side Includes (SSI): SSI is really only intended for simple inclusions, not for "real" programs that use form data, make database connections, and the like.

vs. JavaScript: JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc.

vs. Static HTML: Regular HTML, of course, cannot contain dynamic information.

Disadvantages of JSP

JSP pages require about double the disk space to hold the page.

JSP pages must be compiled on the server when first accessed. This initial compilation produces a noticeable delay when accessing the JSP page for the first time.

JSP Example

We can be choose any smarter way of coding.

I choose NetBeans IDE 8.0.1 for making JSP Programs. It provide a Software Development Platform.

-Before preparing any jsp servlet program. Many things we have to keep in mind that which application server or web server we are using(for example that I am using application server of glassfish)

Program for addition in Jsp.

#addition.jsp

Output

Output:

JSP Program using Request Dispatcher

#newjsp.jsp

Output

Output

Servlet

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.

Advantages of Servlet Servlets provide a way to generate dynamic

documents that is both easier to write and faster to run.

provide all the powerful features of JAVA, such as Exception handling and garbage collection.

Servlet enables easy portability across Web Servers.

Servlet can communicate with different servlet and servers.

Since all web applications are stateless protocol, servlet uses its own API to maintain  session

Disadvantages of Servlets

Designing in servlet is difficult and slows down the application.

Writing complex business logic makes the application difficult to understand.

You need a Java Runtime Environment on the server to run servlets. 

Servlet Example:Program for addition in Servlet

#addition.java(Servlet)

Output:

Output:

JSP and Servlet works in Model ,View , Controller

format (MVC format).

Struts

Struts is the most popular framework for developing Java based web applications. Struts is being developed as an open source project started by Apache Software Foundation . Struts framework is based on Model View Controller (MVC) architecture.

Advantages of Struts Centralized File-Based Configuration.

Rather than hard-coding information into Java programs, many Struts values are represented in XML or property files. This loose coupling means that many changes can be made without modifying or recompiling Java code, and that wholesale changes can be made by editing a single file. This approach also lets Java and Web developers focus on their specific tasks (implementing business logic, presenting certain values to clients, etc.) without needing to know about the overall system layout.

Form Beans.In JSP, you can use property="*" with jsp:setProperty to automatically populate a JavaBean component based on incoming request parameters. Unfortunately, however, in the standard API this capability is unavailable to servlets, even though with MVC it is really servlets, not JSP pages, that should usually be the target of form submissions. Apache Struts extends this capability to Java code and adds in several useful utilities, all of which serve to greatly simplify the processing of request parameters.

Bean Tags.Apache Struts provides a set of custom JSP tags (bean:write, in particular) that let you easily output the properties of JavaBeans components. Basically, these are concise and powerful variations of the standard jsp:useBean and jsp:getProperty tags.

HTML Tags.Apache Struts provides a set of custom JSP tags to create HTML forms that are associated with JavaBeans components. This bean/form association serves two useful purposes: It lets you get initial form-field values from Java objects. It lets you redisplay forms with some or all previously entered values intact.

Form Field Validation.Apache Struts has builtin capabilities for checking that form values are in the required format. If values are missing or in an improper format, the form can be automatically redisplayed with error messages and with the previously entered values maintained.    This validation can be performed on the server (in Java), or both on the server and on the client (in JavaScript).

Consistent Approach.Struts encourages consistent use of MVC throughout your application.

Disadvantages of struts Bigger Learning Curve.

To use MVC with the standard RequestDispatcher, you need to be comfortable with the standard JSP and servlet APIs. To use MVC with Struts, you have to be comfortable with the standard JSP and servlet APIs and a large and elaborate framework that is almost equal in size to the core system. This drawback is especially significant with smaller projects, near-term deadlines, and less experienced developers; you could spend as much time learning Struts as building your actual system.

Worse Documentation.Compared to the standard servlet and JSP APIs, Struts has fewer online resources, and many first-time users find the online Apache documentation confusing and poorly organized. There are also fewer books on Apache Struts than on standard servlets and JSP.

Less Transparent.With Struts applications, there is a lot more going on behind the scenes than with normal Java-based Web applications. As a result, Struts applications are: Harder to understand Harder to benchmark and optimize

Rigid Approach.The flip side of the benefit that Struts encourages a consistent approach to MVC is that Struts makes it difficult (but by no means impossible) to use other approaches.

Struts Example with Beginning: Program for inserting data in

Database

Struts Project Name

Set Server

Add Struts Framework

These things get automatically added when I click finish button

These things are added by me.

IN THE WEBPAGES FOLDER Insertbook.jsp Insertsuccess.jsp Error.jsp IN THE com.myapp.struts Bookaction.java Bookactionform.java IN THE LIBRARY Sqljdbc.jar

#Insertbook.jsp

#Insertsuccess.jsp

#error.jsp

For creating bookaction.java

For creating bookaction.java

For creating bookaction.java

#bookaction.java

#bookaction.java

#bookaction.java

#bookaction.java

I made database login in the SQL Server Management Studio where I

made rows and columns for the insertion

#bookactionform.java

#bookactionform.java(for creating bookactionform(Strutsactionfrom) we have to follow same procedure like bookaction(Strutsaction))

#bookactionform.java

#bookactionform.java

#ApplicationResource.properties

#struts-config.xml

Deploy the strutsprog

Output

Output

Checking the insertion in the Database

Conclusion

In JSP I show that how we do backend coding or how to make business logic which is not possible in simple html page.

In Servlet I show similar type of coding which is performed by me in jsp pages. I do coding inside the processrequest which contain both method of doget and dopost.

In the Struts I show how to add validation in the jsp page(or any view page).And how to connect these pages to the Database that in my project I connect Insertbook.jsp page to the Database Login.

THANK-YOU