TechnoBuzz

A Techno Blog, mainly about Java

Looping Through the Forms and Elements Array

Link: Looping Through the Forms and Elements Array.

Usefull for debugging html/jsp

March 30, 2005 Posted by | Servlets & JSP | Leave a comment

Thread safe issues in Servlets

After browsing thru the book Head First Servlets and JSP , I have to say the book is quite good. Even though I have no interest in the Sun Certified Web Component Developer Exam, the way it presents the material is quite different than what I am used to.
The JavaCamp site has some things to be aware of while developing servlets. Things like sessions and global class instance files not being thread safe. This site had some information on the topic as well. Agaiin, its notes for the SCWCD exam.
The Online version of the book Java Servlet programming is a good reference. Lastly, as this article states : "thread safety is an issue you must contend with regardless of your application type or project size"

In the servlet model (multithreaded) , container maintains a thread pool (threads waiting to execute some code).  The way it work is that there is a dispatcher thread that manages these threads in the pool (worker threads).

When container receives a request for a servlet, the dispatcher thread picks picks up a worker thread from the pool, and the worker thread then executes the service() method of the servlet.

If the container receives a new request while the thread is still working, it will pick up the new thread from the pool. It does not matter if the new thread is for the same servlet, the container just does its job of executing multiple threads.

The single thread model is used for tasks such as things that must be excuted sequentially and not simultaneously.  Basically, the specification says that it will not exceute the service() routine in more than one thread at the same time. However, the container can create multiple instances of a servlet if the servlet implements the single thread model. The single thread model should be avoided if possible.

Thread safety tips:

  • local variables: each thread gets its own copy of the local variables.
  • instance variables: not thread safe. There is only one copy of the instance variable per instance of the servlet. Use instance vriables for variables that can be initialized at servlet startup and can be used througout life of the servlet (i.e connection variable).
  • class variables: not thread safe also. Use static on variables where value remains constant.
  • context scope (getServletContext()): not thread safe
  • session: not thread safe also. try synchronized (session){}
  • request:  thread safe. Paased as a a parm of the service() method.

November 15, 2004 Posted by | Servlets & JSP | 79 Comments

OLAP JSP

JPIVOT – JPivot is a JSP custom tag library that renders an OLAP table and let users perform typical OLAP navigations

November 8, 2004 Posted by | Servlets & JSP | Leave a comment

JavaBeans

Beans
– each property is a variable name that begins with lower case variable
– Each accessor method (getter/setter) starts with the word set, get, or is.
– in using a bean, it has attributes:
  id : variable name in the page
  name : Java class /name of bean
  property : property name from bean , i.e getName
  scope : variable name in the page

October 7, 2004 Posted by | Servlets & JSP | Leave a comment

Servlets – example1

Servlet model which others will extend:

public abstract class AbbyServlet extends HttpServlet {

protected void forward(HttpServletRequest httpservletrequest,
HttpServletResponse httpservletresponse,
String s) throws ServletException, IOException {
RequestDispatcher requestdispatcher = getServletContext().getRequestDispatcher(s);
requestdispatcher.forward(httpservletrequest, httpservletresponse);
}

protected void checkSession(HttpServletRequest httpservletrequest,
HttpServletResponsehttpservletresponse)
throws specSessionException, specHttpSessionException {
try {
specSession ssession = null;
if(httpservletrequest.getSession() == null)
throw new specHttpSessionException();
ssession = getspecSession(httpservletrequest);
if(ssession == null)
throw new specSessionException();
catch(IllegalStateException illegal1) {
throw new specSessionException(illegal1.getMessage());
}

protected specSession getspecSession(HttpServletRequest httpservletrequest) {
HttpSession httpsession = null;
try {
httpsession = httpservletrequest.getSession();
}
catch(Exception exception) {
System.out.println(“SESSION ERROR: getSession(): ” + exception.getMessage());
}
if(httpsession == null) return null;
Object obj = null;
try {
obj = httpsession.getValue(“theSession”);
}
catch(Exception exception1) {
System.out.println(“SESSION ERROR: getValue(): ” + exception1.getMessage());
}
if(obj instanceof specSession)
return (specSession)obj;
else return null;
}

protected void setspecSession(specSession ssession, HttpServletRequest httpservletrequest) {
HttpSession httpsession = httpservletrequest.getSession();
httpsession.putValue(“theSession”, ssession);
}

}

September 4, 2004 Posted by | Servlets & JSP | Leave a comment

Apache Taglibs (JSTL)

In creating a dynamic project in eclipse (File -> New Project -> Web -> Dynamic web project) it gives you the option of adding struts support and taglibs support.

The Jakarta Taglibs is a free, open-source implementation of tag libraries developed under the Jakarta project at the Apache Software.
Foundation.

The web.xml file looks like this for struts and taglibs setup:

<?xml version=”1.0″ encoding=”UTF-8″?>
&lt!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application
2.3//EN” “http://java.sun.com/dtd/web-app_2_3.dtd”&gt;

<web-app id=”WebApp” >
<display-name>its</display-name >
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>validate</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>



<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-template.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-template.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/application-1.0</taglib-uri&gt;
<taglib-location>/WEB-INF/lib/taglibs-application.jar</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/page-1.0</taglib-uri&gt;
<taglib-location>/WEB-INF/lib/taglibs-page.jar</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/request-1.0</taglib-uri&gt;
<taglib-location>/WEB-INF/lib/taglibs-request.jar</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/response-1.0</taglib-uri&gt;
<taglib-location>/WEB-INF/lib/taglibs-response.jar</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/session-1.0</taglib-uri&gt;
<taglib-location>/WEB-INF/lib/taglibs-session.jar</taglib-location>
</taglib>
<taglib>
<taglib-uri>jspsql</taglib-uri>
<taglib-location>/WEB-INF/lib/jspsql.jar</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/i18n-1.0</taglib-uri&gt;
<taglib-location>/WEB-INF/lib/i18n.jar</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/datetime-1.0</taglib-uri&gt;
<taglib-location>/WEB-INF/lib/taglibs-datetime.jar</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/string-1.0.1</taglib-uri&gt;
<taglib-location>/WEB-INF/lib/taglibs-string.jar</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/utility</taglib-uri >
<taglib-location>/WEB-INF/lib/utility.jar</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/mailer-1.1</taglib-uri&gt;
<taglib-location>/WEB-INF/lib/taglibs-mailer.jar</taglib-location>
</taglib>

August 24, 2004 Posted by | Servlets & JSP | 1 Comment

JSTL

JavaServer Pages Standard Tag Library (JSTL)
.

It says the the jwsdp kit which I downlaoded recently includes JSTL. Since the Web Services Pack supports a JSP 2.0 container and makes all JSTL jar files globally available to all web applications, you are all set to immediately take advantage of JSTL 1.1. In the JSTL lib folder within jwsdp their is a jstl.jar file.

The constituent tag libraries of Standard Taglib are as follows:

1. Core <c:tagname … >

2. XML processing <x:tagname … >

3. I18N capable formatting <fmt:tagname … >

4. Database access (SQL) <sql:tagname … >

5. EL Functions fn:fnname …

August 11, 2004 Posted by | Servlets & JSP | Leave a comment

Servlets – reference info

Introduction to servlets

JSP Expression  <%= expression %> Expression is evaluated and placed in output.

JSP Scriptlet  <% code %> Code is inserted in service method.

JSP Declaration <%! code %> Code is inserted in body of servlet class, outside of service method.

JSP page Directive <%@ page att="val" %>  Directions to the servlet engine about general setup.

Implicit objects:

  • import="package.class"
  • contentType="MIME-Type"
  • isThreadSafe="true|false"
  • session="true|false"
  • buffer="sizekb|none"
  • autoflush="true|false"
  • extends="package.class"
  • info="message"
  • errorPage="url"
  • isErrorPage="true|false"
  • language="java"

JSP comment <%– comment –%> Comment; ignored when JSP page is translated into servlet.

Several local variables are automatically declared by JSP:

  • request – javax.servlet.http.HttpServletRequest
  • response –  javax.servlet.http.HttpServletResponse
  • session – javax.servlet.http.HttpSession
  • config – javax.servlet.SErveltConfig
  • application – javax.servlet.ServletContext
  • out – javax.servlet.jsp.JspWriter

JavaServer Pages (JSP) technology provides you with a simple way to
build and maintain web pages with dynamically generated content.

JSP technology allows you to change the code that deals with the
dynamic content of a web page while leaving the HTML that deals
with the static content unchanged.

Your JSP page includes both standard HTML or XML tags and
special JSP tags.

JSP technology supports both standard and user-defined JSP tags.

JSP 101
A beginners guide.

JSPTutorial
JSP tutorial starts from simple examples and progresses to complex concepts.

Java Server Pages
Tutorial based on Sun Core Servlets and JSP book.
Servlet section is here.

Two key HTTP Request methods:

GET – Get a file and return its contents. In the context of a servlet, means send some dynamic content to the user as html.

POST – Used to send data to the server. It happens usually when you type some information into a html form and click the submit button.   

HTTP Responses:

Contains the HTTP version followed by a numeric 3 digit status code, and its associated textual phrase (for debugging purposes).
1xx :Informational – request recieved, continuing process
2xx : Success – action was successfully recieved, understood, and accepted.
3xx : Redirection: further action needed to complete the request.
4xx : Client Error – request contains bad syntax or cannot be fulfilled.
5xx: Server Error – sever failed to fulfill an apparently valid request.

/servlet/ part of the client URL.

javax.servlet package – Contains the Servlet interface.

Most servlets extend either GenericServlet or HttpServlet and override some or all of their methods with appropriate customized behaviors:

javax.servlet.GenericServlet- Defines a generic, protocol-independent servlet. It implements the Servlet and ServletConfig interfaces.To write a generic servlet, you need only override the abstract service method.

javax.servlet.http.HttpServlet – Web based servlets typically extend class HttpServlet class and must override at least one method, usually one of these:

  • void init( servletConfig config ) – this method is automatically called once during the servlet’s execution cycle to initialize the servlet.
  • void service( ServletRequest request, ServletResponse response ) – this is the first method called on every servlet to respond to a client request
  • void destroy() – this cleanup method is called when servlet is terminated by the server on which it is executing.
  • String get ServletInfo() – this method is defined by a servlet programmer to return String containing servlet information such as the servlet’s author and version.
  • doGet( ServletRequest request, ServletResponse response ), responds to an HTTP Get requests from a client, gets (or retrieves) information from the server (e.g., an HTML document or an image).
  • doPost( ServletRequest request, ServletResponse response ), respond to a HTTP Post requests from a client, a post request posts (or sends) data to the server (i.e., to send the server information from a HTML form in which the client enters data) so it can search the internet or query a database. 

The arguments HttpServletRequest and HttpServletResponse:

javax.servlet.http  (HttpServletRequest Interface) – Extends the ServletRequest interface:
getParameter – Returns the value of a request parameter. For HTTP servlets, parameters are contained in the query string or posted form data.
getAttribute – Returns the value of the named attribute as an Object.
setAttribute – Stores an attribute in this request.

javax.servlet.http  (HttpServletResponse Interface) – Extends the ServletResponse interface:
setContentType – Sets the content type of the response being sent to the client. The content type may include the type of character encoding used, for example, text/html;
Requests between servlets
jsp fwd action

August 11, 2004 Posted by | Servlets & JSP | Leave a comment