Servlets and JSP Pages Best Practicesby Qusay H. Mahmoud. March 2. Java Servlet technology and Java. Server Pages (JSP pages) are server- side technologies that have dominated the server- side Java technology market; they've become the standard way to develop commercial web applications. Java developers love these technologies for myriad reasons, including: the technologies are fairly easy to learn, and they bring the Write Once, Run Anywhere paradigm to web applications.
But here, we are mixing the database logic in the servlet only for simplicity of the program. We will develop this page in JSP following DAO, DTO and Singleton design pattern later. Example of Registration form in servlet In this example, we have created the three. Example of Login form in Servlet Tutorial. Let's see a simple example of login and logout in servlet. Servlets Programming Tutorial - Login Page Project The code is available here : http:// For More Video Tutorials Visit : http:// Another Channel : http:// Facebook Page : http://www.facebook.com/VNRgroups. Servlet, login form, Login Example, Login application, code, login, form, WelcomeServlet, example, program, eclipse. Project Structure: Reqst.java: package com.candid; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class. JSP - Servlets: Full Login Example In this section we are going to discuss the implementation of a complete Login application (We received the source code of this application through Amira Thabet. Here is the link for whole code in this playlist. Login Form creation and Authenticate users. Using JDBC and JSP performing these operations.
Servlets - Database Access Servlets - File Uploading Servlets - Handling Date Servlets - Page Redirect Servlets - Hits Counter. Free source code and tutorials for Software developers and Architects.; Updated. Hey, I have an assignment of creating a login page.if the username and password is dolly it should redirect me to another servlet page telling valid user else it should. A request line That is the call for a URL, referring to a certain page or a servlet A typical Request example: GET /path/to/file/index.html?parameter=value HTTP/1.0 Dissecting the 'RequestParamExample.java ' I use 'GET' method to inspect the query string in the URL. Use 'POST' method for production. No 'action' attribute is specified in the <form> tag. The default 'action' is the current page. The query string comprising name=value.
More importantly, if used effectively by following best practices, servlets and JSP pages help separate presentation from content. For instance, embedded Java code (scriptlets) in sections of HTML documents can result in complex applications that are not efficient, and difficult to reuse, enhance, and maintain. Best practices can change all that.
In this article, I'll present important best practices for servlets and JSP pages; I assume that you have basic working knowledge of both technologies. This article: Presents an overview of Java servlets and Java.
Server pages (JSP pages)Provides hints, tips, and guidelines for working with servlets and JSP pages. Provides best practices for servlets and JSP pages. Overview of Servlets and JSP Pages. Similar to Common Gateway Interface (CGI) scripts, servlets support a request and response programming model.
When a client sends a request to the server, the server sends the request to the servlet. The servlet then constructs a response that the server sends back to the client. Unlike CGI scripts, however, servlets run within the same process as the HTTP server. When a client request is made, the service method is called and passed a request and response object. The servlet first determines whether the request is a GET or POST operation. It then calls one of the following methods: do.
Get or do. Post. The do. Get method is called if the request is GET, and do. Post is called if the request is POST. Both do. Get and do. Post take request ( Http. Servlet. Request) and response ( Http.
Servlet. Response). In the simplest terms, then, servlets are Java classes that can generate dynamic HTML content using print statements. What is important to note about servlets, however, is that they run in a container, and the APIs provide session and object life- cycle management. Consequently, when you use servlets, you gain all the benefits from the Java platform, which include the sandbox (security), database access API via JDBC, and cross- platform portability of servlets.
Java. Server Pages (JSP)The JSP technology- -which abstracts servlets to a higher level- -is an open, freely available specification developed by Sun Microsystems as an alternative to Microsoft's Active Server Pages (ASP) technology, and a key component of the Java 2 Enterprise Edition (J2. EE) specification. Many of the commercially available application servers (such as BEA Web. Logic, IBM Web. Sphere, Live JRun, Orion, and so on) support JSP technology.
How Do JSP Pages Work? A JSP page is basically a web page with traditional HTML and bits of Java code. The file extension of a JSP page is . When a JSP page is called, it will be compiled (by the JSP engine) into a Java servlet. At this point the servlet is handled by the servlet engine, just like any other servlet. The servlet engine then loads the servlet class (using a class loader) and executes it to create dynamic HTML to be sent to the browser, as shown in Figure 1.
The servlet creates any necessary object, and writes any object as a string to an output stream to the browser. Figure 1: Request/Response flow calling a JSP page. The next time the page is requested, the JSP engine executes the already- loaded servlet unless the JSP page has changed, in which case it is automatically recompiled into a servlet and executed.
Best Practices. In this section, I present best practices for servlets and particularly JSP pages. The emphasis on JSP best practices is simply because JSP pages seem to be more widely used (probably because JSP technology promotes the separation of presentation from content). One best practice that combines and integrates the use of servlets and JSP pages is the Model View Controller (MVC) design pattern, discussed later in this article. Don't overuse Java code in HTML pages: Putting all Java code directly in the JSP page is OK for very simple applications. But overusing this feature leads to spaghetti code that is not easy to read and understand.
One way to minimize Java code in HTML pages is to write separate Java classes that perform the computations. Once these classes are tested, instances can be created. Once such content is in separate files, they can be included in all pages using one of the following include mechanisms: Include directive: < %@ include file=. I'd recommend using the include directive, which is fast in terms of performance, if the file doesn't change often; and use the include action for content that changes often or if the page to be included cannot be decided until the main page is executed. You can use this tag to bring in, or import, content from local and remote sources.
Here are some examples. Separating business logic from presentation permits changes to either side without affecting the other. However, production JSP code should be limited to front- end presentation. So, how do you implement the business logic part? That is where Java. Beans technology comes into play.
This technology is a portable, platform- independent component model that lets developers write components and reuse them everywhere. In the context of JSP pages, Java. Beans components contain business logic that returns data to a script on a JSP page, which in turn formats the data returned from the Java. Beans component for display by the browser.
A JSP page uses a Java. Beans component by setting and getting the properties that it provides. The benefits of using Java. Beans components to augment JSP pages are: Reusable components: Different applications will be able to reuse the components. Separation of business logic and presentation logic: You can change the way data is displayed without affecting business logic. In other words, web page designers can focus on presentation and Java developers can focus on business logic.
Protects your intellectual property by keeping source code secure. If you use Enterprise Java. Beans (EJBs) components with your application, the business logic should remain in the EJB components which provide life- cycle management, transaction support, and multi- client access to domain objects (Entity Beans).
Please refer to the Enterprise Blue. Prints for further details on this.
While Java. Beans components can be used to encapsulate much of the Java code, using them in JSP pages still requires content developers to have some knowledge of Java syntax. As a Java developer, you can extend JSP pages by introducing custom tags that can be deployed and used in an HTML- like syntax. Custom tags also allow you to provide better packaging by improving the separation between business logic and presentation logic. In addition, they provide a means of customizing presentation where this cannot be done easily with JSTL.
Any necessary parameters to the tag can be passed as attributes or body content, and therefore no Java code is needed to initialize or set component properties. They have simpler syntax. Scriptlets are written in Java code, but custom tags can be used in an HTML- like syntax. They can improve the productivity of nonprogrammer content developers, by allowing them to perform tasks that cannot be done with HTML. They are reusable. They save development and testing time.
Scriptlets are not reusable, unless you call cut- and- paste . Check to see if what you want is already available. Do not reinvent the wheel: While custom tags provide a way to reuse valuable components, they still need to be created, tested, and debugged. In addition, developers often have to reinvent the wheel over and over again and the solutions may not be the most efficient. This is the problem that the Java. Server Pages Standard Tag Library (JSTL) solves, by providing a set of reusable standard tags. JSTL defines a standard tag library that works the same everywhere, so you no longer have to iterate over collections using a scriptlet (or iteration tags from numerous vendors).
The JSTL includes tags for looping, reading attributes without Java syntax, iterating over various data structures, evaluating expressions conditionally, setting attributes and scripting variables in a concise manner, and parsing XML documents. An expression language (EL), which is designed specifically for page authors, promotes JSP scoped attributes as the standard way to communicate information from business logic to JSP pages. Note, however, that while the EL is a key aspect of JSP technology, it is not a general purpose programming language. Rather, it is simply a data access language, which makes it possible to easily access (and manipulate) application data without having to use scriptlets or request- time expression values.
For example, to access a simple variable, you can use something like. Tags: a. Tag attribute=. If you ever come across a situation where you have several servlets or JSP pages that need to compress their content, you can write a single compression filter and apply it to all resources. In Java Blue. Prints, for example, filters are used to provide the Sign. On. To maximize the portability of your enterprise application, use a portable web application security model.
In the end, however, it's all about tradeoffs. For example, if you have a predefined set of users, you can manage them using form- based login or basic authentication.
But if you need to create users dynamically, you need to use container- specific APIs to create and manage users.