Suppose you are familiar with some beginner concepts regarding Servlet and Java Server Pages (JSP), this article focuses on some techniques for making JSP and Servlet working together in a Java web application. A brief overview of Servlet and JSP technologies
A Servlet is a Java class running on a web server to serve client’s requests. Servlets are commonly used to handle HTTP requests. They are created to address shortcomings of CGI scripting, and enhance functionality
• Cache static data using jspInit() method.
• Release static data in jspDestroy() method.
• To concatenate string use, StringBuffer.
• Do not use println() method.
• Do not use PrintWriter to send binary data. Use ServletOutputStream.
• Do not use sybchronized blocks in service method.
• Do not create session object with directive <%@ page session="false" %>
• Scope of ‘useBean’ action should be minimized.
• Minimize the use
No, in JSP the variable declaration is not at all thread safe as the declared variables are ended up as instance variable in generated servlet not in body of _jspservice() method.
The given declaration is not considered to be thread safe: as they are declarations and are evaluated only one time when page gets loaded.
The given declaration is considered thread safe: as in the scriplets the variables that have been declared are not shared
An output comment is sent to the client where it becomes viewable in the source of browser. <!-- This is a comment which is sent to client -->
JSP page is documented by hidden comment however it is not sent to client. Hidden comment is ignored by the JSP engine and no code is processed in tags of the hidden comments. <%-- This comment will not be visible to client --%>
Updated 01-15-2012 at 07:43 AM by JSP
Pre-translated: Before the JSP file is compiled or translated, into the Servlet.Translated: The JSP file is being compiled & translated as a Servlet.Initialized: Before handling requests of service method, the container calls the jspInit() to do the initialization of the Servlet. Called just one time per Servlet instance.Servicing: client requests are served. For each request, container calls it.Out of service: The Servlet instances are out of their services. The container calls
Java Servlet technology’s extension is Java ServerPages. JSP is used as presentation layer to combine the Java code and HTML. Java Servlet technology generates HTML along with out.println(“<html>….. </html>”) statements (out is PrintWriter). Embedding HTML code to the escape characters is a process which is hardly maintained and is cumbersome. A level of abstraction is provided by the JSP technology in order to enable a developer to use the action elements and custom tags. This makes
Declarative security is implemented by the Servlet containers. Administration takes place via deployment descriptor web.xml file. Servlets & JSP pages along with the declarative security will become security aware code free. URLs could be protected via web.xml as it has been shown below: XML Code: This is a web.xml with the security tags <web-app>
Updated 01-14-2012 at 05:52 PM by Servlet
Cookies are text files that are downloaded onto a visitor's hard drive to store the visitor's actions in order to better
customize their following visits. We may handle cookies in JSP and I will show you how to do that.
JSP provides an implicit response object which is used to add cookies. To add a cookie to the response, addCookie method is used.
We may store useful information in cookies for later use and this method is perfect for our needs. Java Code: response.addCookie(Cookie