jump to navigation

Servlet Examples August 18, 2005

Posted by Coolguy in Servlets/Jsp.
trackback

Hello world

package test;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Hello extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{

public Hello()
{
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out =response.getWriter();
out.println(“Hello”);
out.close();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
}

}

Snoop ~ Request Dispatcher Example

Servlet

package test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Snoop extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
public Snoop() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userAgent = request.getHeader(“user-agent”);
String clientBrowser = “Not known!”;
if( userAgent != null)
clientBrowser = userAgent;
request.setAttribute(“client.browser”,clientBrowser );
request.getRequestDispatcher(“/showBrowser.jsp”).forward(request,response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}

JSP

<%@

page language=“java” contentType=“text/html; charset=ISO-8859-1”pageEncoding=“ISO-8859-1”%>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
>
<
html><head><meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1”
>
<
title>Insert title here</title
>
</
head><body
>
<
table border=“1”> <tr> <th>Header</th><th>Value</th> </tr> <tr>

<td>user-agent</td> <td><%= request.getAttribute(“client.browser”)%></td> </tr></table
>
</
body

>
</
html>

web.xml for both

<?xml version=“1.0” encoding=“UTF-8”?>
<
web-app>
<display-name>HelloWorld</display-name>

<servlet>
<description></description>
<display-name>Snoop</display-name>
<servlet-name>Snoop</servlet-name>
<servlet-class>test.Snoop</servlet-class>
</servlet>

<servlet>
<description>Says Hello</description>
<
display-name>Hello</display-name>
<servlet-name>Hello</servlet-name>
<servlet-class>test.Hello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Snoop</servlet-name>
<url-pattern>/Snoop</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

</

web-app>

Path to test

http://localhost:8080/HelloWorld/Hello

http://localhost:8080/HelloWorld/Snoop

Form Processing Servlet

Servlet

package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class for Servlet: Guest
*
*/
public class Guest extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public Guest() {
super();
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String firstname =request.getParameter(“firstname”);
String lastname =request.getParameter(“lastname”);
PrintWriter out= response.getWriter();
response.setContentType(“text/html”);
out.write(“Hello”+lastname+firstname);
}
}

InitParam

package test;
import java.io.IOException;

import java.io.PrintWriter;
import javax.servlet.GenericServlet;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class InitParamTest extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

public InitParamTest() { super(); }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

response.setContentType(“text/plain”);

PrintWriter out = response.getWriter();

out.println(“name” + “: ” + getInitParameter(“myname”));

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{ // TODO Auto-generated method stub }

public void init(GenericServlet ServletConfig) throws ServletException

{

super.init();

String name=ServletConfig.getInitParameter(“myname”);

System.out.println(“Initialising InitParamTest”);

}

}

Web.xml

<servlet>
<description></description
>
<display-name>InitParamTest</display-name
>
<servlet-name>InitParamTest</servlet-name
>
<servlet-class>test.InitParamTest</servlet-class
>
<init-param
>
<description>test parameter</description
>
<param-name>myname</param-name
>
<param-value>DummyValue</param-value
>
</init-param
>
</servlet>

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: