Sunday, January 25, 2015

The JSP life cycle

A JSP life cycle can be defined as the entire process from its creation till the destruction which is similar to a servlet life cycle with an additional step which is required to compile a JSP into servlet.

There are several main processe that will be performed in a jsp life cycle

CompilationInitializationExecutionCleanup



These are much similar to the servlet life cycle

Saturday, January 24, 2015

Creating a file upload form using HTML and JSP



Follow the steps below to create a file upload form using a web application that includes HTML and JSP. In this form we must use the POST() method and not the GET() method. The form action attribute should be set to a JSP file that will handle the uploading process.
To upload a single file you should use a single <input .../> tag with attribute type="file". To allow multiple files uploading, include more than one input tags with different values for the name attribute. The browser associates a Browse button with each of them.


Below is the coding for the HTML file

<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload by CodeMonkeyy</h3>
Select a file to upload: <br />
<form action="FileUpload" method="post"
                        enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

Friday, January 23, 2015

Creating a simple snake game using the HTML5 canvas


Here we are using the new canvas feature in HTML5 to create a simple snake game we used to play in our childhood.



First create a canvas in your HTML page

<canvas id="drawCanvas" width="600" height="600"></canvas>


Then we declare some variables we need in our cording

var drawCanvas = $("#drawCanvas")[0];
            var context = drawCanvas.getContext("2d");
            var width = $("#drawCanvas").width();
            var height = $("#drawCanvas").height();

Thursday, January 22, 2015

Creating a simple login form using HTML and JSP with error handling

Here in this tutorial a simple login form is created for the user to enter the username and the password to login into the web application.

This is the HTML part of the web application, here the interface of the application is created using HTML forms. The code and the screen shot is provided below.

HTML


Using HTML5 canvas to create a simple game.

Here  a simple game is made using HTML5 , follow the steps to create the application.
The HTML page is used to create the canvas, the coding for the HTML page is given below


The above screen shot shows the canvas created using HTML


Wednesday, January 21, 2015

Steps to create a simple web application using HTML and JSP (Simple Calculator)



Here a simple calculator is designed using HTML and jsp. The design is made using HTML and the a jsp code is used to perform the calculation

The below screen shot shows the html code which is used to create the interface of the application.

Introduction to JSP



JavaServer Pages — JSP is a Java based technology that is used to develop dynamic web sites. With JSP, web designers and developers can quickly incorporate dynamic elements into web pages using embedded Java and markup tags. These tags provide the HTML designer with a way to access data and business logic stored inside Java objects

The following figure shows the process of the flow of events that occur after a client 
requests for a JSP page



Tuesday, January 20, 2015

Simple HTML tags



<!-- -->                           to comment in html
<a>                                        anchor tag-used for hyperlinks
<align>                              align tag to align text
<body>                                 defines the boundaries of the documents body
<br>                                     a line break
<center>                             center the content
<form>                                  define a form
<h1>                                       first level heading tag
<head>                                  defines the boundaries of the documents head
<html>                                  defines the boundaries of the html document
<input type>                defined an input widget to a form
<p>                                          a new paragraph
<title>                                the documents title.

Creating a simple web application in netbeans to understand the use of jsp

Follow these steps to create a simple web application, here a simple web application is created step by step in order to get a sound knowledge  about the netbeans IDE and how jsp and web applications work.




What is a container?


Servlets do not have a main method, they are under the control of another java method called the container. Tomcat is an example of a container, the server hands the request to not only the servlet, but also the container. It’s the container that gives the servlet the request and the response, and it’s the container that calls the servlets method.

The containers handle a request when it sees that the request is for a servlet then the container creates a hhtpServletResponse and httpServletRequest objects. The container finds the correct servlet based on the URL in the request, creates or allocates a thread, and passes the request and the response to the servlet thread. Then the container calls the servlets Service() method depending on the request and the Service() method calls either the doGet() or doPost() method. According to the method a dynamic page will be generated and it will be stuffed into the response object, then the container converts the response object into a HTTP response.

What is the HTTP protocol?


HTTP runs on the TCP/IP, TCP/IP makes sure that the file sent from one network is full sent to the other side. Even if the file may be sent in chunks when it is sent.
GET and POST are two http request methods to tell the server what kind of request is made and how the rest of the message is formatted. GET is an http method and its main objective is to ask the server to get a particular request and send it back. The POST method is a more powerful method which is able to get data like the GET method and as well send form data to the server. These two methods are not the only two methods that are used, there are many other methods too but these are the two most commonly used methods.

 Can the GET method send data too?
YES the get method can also send data but you better use POST instead of the get method.
Reasons why a GET method should not be used to send data.

·   (1) A GET method may have a limited amount of characters and so for example a passage cannot be sent using a GET method.
·   (2)The data sent using the GET method is shown in the url so it is exposed to everyone.

What is a TCP port?

A 16-bit number that identifies a specific software program on the server hardware.  Think of the portas a unique identifier. Without a port number the server will not have any way of knowing which application need to be connected.

What web servers and clients do?


The web browsers allow users to send a request and what the serves do is, get the request, find the resource and return something to the user. This resource can differ. Unless the resource is not found or the resource is not at the expected place, the “404 Not Found” error will be displayed as the server can’t find the required resource. The client request contains the name and the address of the resource the client is searching for.
What does a web client do?
A web client lets the user request something on the server and show the user the result of the request.


Monday, January 19, 2015

configuring Apache Tomcat as a server in netbeans

Follow the steps below to configure apache tomcat as a server in netbeans

1) In netbeans IDE click click the tools drop down menu and select servers.

Installing Tomcat

Follow the steps below to install tomcat on your computer

1) Download Apache Tomcat from the apache website, the link is provided below.
http://tomcat.apache.org/download-70.cgi



2)Extract the .zip file and copy it to any place of your own choice


Sunday, January 18, 2015

Methods used in Servlets

There are many methods that are used in creating servlets.


Init() Method

the following code shows how the init() method is used.
public void init (ServletConfig config) throws ServletException


The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.
The servlet container cannot place the servlet into service if the init method
  1. Throws a ServletException
  2. Does not return within a time period defined by the Web server

getServletConfig() Method

public Servletconfig getServletConfig()

Returns a Servletconfig object, which contains initialization and startup parameters for this servlet. The ServletConfig object returned is the one passed to the init method.

getservletinfo() Method

public java.lang.String getServletInfo()

Returns information about the servlet, such as author, version, and copyright.
The string that this method returns should be plain text and not markup of any kind (such as HTML, XML, etc.).

doGet() Method

The get method is used to send a request for example when typing in a url into a browser.Here the browser simply requests the contents of a particular URL. When the browser uses this method, the web server ends up calling the servlet's doGet() method.

doPost() Method

When data entered to a web form is sent to a particular URL, the the doPost() method is used.
A POST request is a request to post (to send) form data to a resource on the server. This is the case of of a form in a web page declared this way in html: <form method="POST">. In this case the size of the parameters can be much greater.


doHead() Method

The doHead() method handles requests sent using the HTTP HEAD method. Similar to the GET method, the HEAD method also sends requests to the server. The only difference between the GET and the HEAD methods is that the HEAD method returns the header of the response, which contains entries, such as Content-Type, Content-Length, and Last-Modified.


destroy() Method

public void destroy()

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.


How Servlets Work

The interaction between the J2EE server and the web container

A servlet is an object that extends either the javax.servlet.GenericServlet class or the javax.servlet.http.HttpServlet class. The javax.servlet.GenericServlet class defines methods for building generic, protocol-independent servlets. The javax.servlet.http.HttpServlet class extends this class to provide HTTP-specific methods.




Creating the path and the class path

Follow these steps to create the path and the class path



1) Goto system properties and the select "Advance System Settings"


2) Click the "Advanced" Menu and the select "Environmental Variables". Then the below window will open


3) Then click new under "User Variables for Administrator"



4) The above shown window will open. Copy the path of the jdk bin file and the appserver bin file.
Eg-C:\Program Files\Java\jdk1.7.0_40\bin;C:\Sun\AppServer\bin;


5) Then in the same manner set the "classpath" here set the path of the "lib" file and the "j2ee.jar" file in the appserver file.
Eg-C:\Program Files\Java\jdk1.7.0_21;C:\Sun\AppServer\lib\j2ee.jar


6) Then click "OK" to finish



Installing Application Server PE


Follow the below steps to install Application Server PE













To start the server go to

start > All Programs > Sun Microsystems > Application Server PE > Start Default Servers
The below screenshot shows how to start the server.


Then set the path and the class path.

Introduction to Java Servlets

Java Servlets are server side Java programs that require either a Web Server or an Application Server for execution. 

First download the free Sun Java System Application Server PE 8.2 from the sun's website
http://java.sun.com/j2ee/1.4/download.html

Go to the above website and follow the steps as shown below to download the system application server.


The link will be redirected to the oracle website and a page like the above screen shot  , click the download button in front of the "sun Java system application server" text.


Then the above web page will open, here accept the license agreements and then download the required application.



Introduction to J2EE

Early web sites were collections of web pages linked together by Hypertext Markup Language (HTML). Today, websites feature multimedia, e-commerce applications, and other sophisticated web-based applications such as voice over IP and online banking. In addition, there have been advances in web server technologies such as the development of application servers, and dynamic content creation technologies like Java Server Pages (JSP) and Active Server Pages (ASP).

Download the JDK from sun website-http://java.sun.com/j2ee/1.4/download.html

Next-Servlets