How To Set up Cookies With Servlet

Talk is cheap. Show me the code. ~ Linus Torvalds

A browser cookie is a small piece of data sent from website and stored in user’s web browser while the user is browsing a given website. Every time user loads the website, browser sends the cookie back to the server to notify the website of the user’s previous activity.

We are going to learn how to set up cookies with servlet. We are going to create three files:

  • HelloForm.java (servlet).
  • HelloForm.html
  • ReadCookie.java (servlet).

Creating the first Servlet which is HelloForm.java, here is the code:

Servlet 1

Creating HelloForm.html here is the code

html

Now I want you to run HelloForm.html on your server and you will get something like this:

output

Now I need you to enter First Name and Last Name. After that, click submit. Now we need a way to read the stored names. I am going to use First Name as Henry and Last Name as Mbugua. Lastly, we will create Servlet to read our names.

Creating ReadCookie.java, here is the code:

readcookie

Now we will run ReadCookie.java in our server and see if we can read our stored names. Here is my result:

result

Have something to add, leave me a comment. Happy coding!

Client Request Servlet Demonstration

Good code is its own documentation. As you are about to add a comment, ask yourself, ‘How can I improve the code so that this comment isn’t needed?’ ~ Steve McConnell

When a browser requests for a web page, it sends lot of information to the web server which can not be read directly because this information travel as a part of header of HTTP request. Following is the example which uses getHeaderNames() method of HttpServletRequest to read the HTTP header
information. This method returns an Enumeration that contains the header information associated with the current HTTP request.

Once we have an Enumeration, we can loop down the Enumeration in the standard manner,
using hasMoreElements() method to determine when to stop and using nextElement() method to get each parameter name. Here is the code.

client

Once you are done creating the servlet with the above code, run it in you server and if everything goes right you should get the following result depending on your system.

header

Writing our first Servlet: Hello World

Programs must be written for people to read, and only incidentally for machines to execute ~ Abelson

Servlet are Java classes which service HTTP request and implement the javax.servlet.Servlet interface. Web application developers typically write servlets that extend
javax.servlet.http.HttpServlet, an abstract class that implements the Servlet interface and is specially designed to handle HTTP requests

In our previous lesson we learnt how to link eclipse and web container and now we are ready to create our first Servlet program. I am going to show how to create a dynamic project and a servlet that say Hello World.Launch your eclipse, click on file and choose Dynamic Web Project. You will get a screen like the one shown below:

1

In the project name put henry tutorial. Do not change anything else and click finish. In project explorer, your project will look like this:

2

When you create a servlet it is supposed to go to Java resources. So to create our Hello world servlet. Select Henry Tutorial and right click on it and proceed to new. Click on the servlet and you you will get the following screen.

3

In the Java package put com.henrymbuguakiarie and class name put HelloWorld. So you have the following screen.

4

After you are done with changes click finish and you servlet will open in eclipse editor.

5

Put the code in the image above in your servlet and click save.
Now I want you to go back in your project explorer and expand your project tree like this:

6

To run our HelloWorld.java we need to right click on it select Run As and select Run on Server. If you are successful you will have the following result.

7
Congratulations, you just created your first servlet. Wishing you happy codding.

NB:If you get a server error while running your servlet. The problem could be your server start services when you boot your computer. The solution is to stop your server and run the servlet again. Am a Linux user, just launch your terminal and key in the following command to stop your server.
Sudo service tomcat6 stop. Go back to eclipse and run HelloWorld.java on the server.

How to integrate Eclipse Luna and Tomcat 6 in Linux mint

First, solve the problem. Then, write the code. ~ John Johnson

Using Tomcat within eclipse means all configuration, coding, deployment and management can happen within Eclipse interface. Eclipse offers application server integration via set of plugins called Web Tools Platform. This set of components includes engines for managing, developing, compiling and deploying web artifacts.

NB: This tutorial assumes you have already successfully installed Tomcat on your machine.

Installing WTP

Launch your eclipse, click on help and select Install new software. You will get a screen like this:

1

Click on the add button. You will get the following screen:

2

In the name put web tool platform and location put this url http://download.eclipse.org/webtools/updates and click OK.

Wait for a moment and you will get the following screen:

3

Select web tools platform SDK and click next follow the screen to finish the installation of WPT. After the installation restart your Eclipse for changes to take effect.

Configuring The Tomcat Runtime

Now that we have installed the WPT, we can specify Tomcat as a runtime environment. Setting up a Runtime Environment in Eclipse basically means tellings its engines where to look for your application server, so it can utilizes its components and automatically configure your workspace.

After restarting your Eclipse go to window/preferences/server/runtime. You will get the following screen:

4

Click on add button and select apache you will get the following Screen:

5

Select your installed version of Apache Tomcat and click next. In our next screen you will need to specify the tomcat installation directory. Click on browse and navigate to Tomcat installation directory. You will get the following screen and click finish:

6

Now you need to switch perspective in your Eclipse environment. This is how you do it, go to window/open perspective/other/Java EE . In your console window you will get something like this:

7

Click on the link to create new server: You will get a screen like this:

8

Select your Installed Apache Tomcat version and click finish. Your result will be:

9

NB: For Linux user if you tried to set up you Tomcat installation directory and you got some errors. Something like web apps not found. Here is the solution for you. Launch you terminal windows and key in the following commands.

  • cd /usr/share/tomcat6
  • sudo ln -s /var/lib/tomcat6/conf conf
  • sudo ln -s /var/lib/tomcat6/conf conf
  • sudo ln -s /var/lib/tomcat6/webapps webapps

Congratulation for adding a server runtime in your working environment.

Technology Will Be The Equalizer

It is only when we develop others that we permanently succeed. ~ Samuel Firestone

In present time, things are changing quickly and fast thanks to the technology age that we are living in. We have experienced fast hand what is like not knowing or better yet, what lack of knowledge can do to a person and society at large. But there comes a day when we realized that turning the page is the best feeling in the world, because we realized that there is so much more to the book than the page we are stuck on. May be it is time we turn the page for our country! What if we can apply technology to impact life positively in Northern part of our nation.

I strongly believe that we can change the dynamics of many part of northern part of Kenya especially in Turkana region. I propose that KNEC in conjunction with ministry of education to develop a pilot programme to be tested in Turkana region by offering ICT facilities to the community. Here is my school of thought on the issues:

  • KNEC should develop computers labs in Turkana area.
  • The constructed computer labs should have internet access.
  • Since security is an issue in this areas, the labs should be aligned with Tullow oil companies facilities. This will give proper security, attract education investors and other stake holders.
  • I know the project will cost money and we can achieve this through constituency development fund (CDF).
  • This project will give hope to many children in that region such that in the end, we make a world a better place to live for the next generation.

Kenya claim to be a Technology hub leader in East African, we must live up to that claim. I know this project is not a piece of cake, but the pilot project can be our first step in our journey to prosper as a nation in Technology. It will be a longer and difficult road. We will make mistake along the way but I have discovered that the best view comes after the hardest climb.

Coding our Hello world using Google Web Toolkit

Lesson 3

In this tutorial we are going to edit the following file:

  • HellowGWT.gwt.xml
  • HellowGWT.css
  • HellowGWT.html
  • HelloGWT.java

How to locate the above files in your project. The image below will show you how to do it.

show

Step 1- Modify Module Descriptor: HelloGWT.gwt.xml

GWT plugin will create a default module descriptor file src/com.henrymbuguakiarie/HelloGWT.gwt.xml which is given below. For this example we are going to modifying it to look this way.

xml

Step 2- Modify Style Sheet: HelloGWT.css

css

Step 3 – Modify Host File: HelloGWT.html

hmtl

Step 4- Modify Entry Point: HelloGWT.java

java

Step 5- Compile Application

Once you are ready with all the changes done, its time to compile the project. Use the
option Google Icon box: > GWT Compile Project… to launch GWT Compile dialogue

image4

Keep default values intact and click Compile button. If everything goes fine, you will see
following output in Eclipse console

compile

Step 6- Run the application

Now click on application. Run application menu and select HelloGWT application to run the application. If everything is fine, you must see GWT Development Mode active in Eclipse containing a URL as shown below. Double click the URL to open the GWT application.

developmode

Because you are running your application in development mode, so you will need to install
GWT plugin for your browser. Simply follow the on screen instructions to install the plugin. If
you already have GWT plugin set for your browser, then you should be able to see the
following output:

output

Congratulations! you have implemented your first application using Google Web Toolkit
(GWT). There are a lot of resources over the internet to help you advance your skill. I wish you a happy coding.