Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for access servlet information
Previous Home Next

In this servlet program servlet and server information display on Browser.

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
  <servlet-name>infoServlet</servlet-name>
  <servlet-class>r4r.servlet.infoServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>infoServlet</servlet-name>
  <url-pattern>/infoServlet</url-pattern>
</servlet-mapping>
<session-config>
 <session-timeout>
  30
 </session-timeout>
</session-config>
</web-app>

Index.jsp

<%-- 
Document  : index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" 
    content="text/html; charset=UTF-8">
  <title>r4r.co.in-servlet</title>
 </head>
 <body>
  <form>
  <h1>Access Servlet by servlet's Name!</h1>
 </body>
</html>

Servlet Program

/*
* Save as a infoServlet.java
*/
package r4r.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author R4R
 */
public class infoServlet extends HttpServlet {

private int value = 0;

protected void processRequest(HttpServletRequest request,
	HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// increment value by 1 on every refresh of this page
String count = " " + ++value + " time";
try {
/* TODO output your page here */
out.println("<html>");
out.println("<head>");
out.println("<title>" + getServletInfo() + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet infoServlet at " 
	  + request.getContextPath() + "</h1>");
out.println("<b>About Server</b>");
out.println("<BR>Server Property : " + "<b>"
  + getServletContext().getServerInfo() + "</b>");
out.println("<BR>Server Name : " + "<b>" 
+ getServerName(getServletContext().getServerInfo()) + "</b>");
out.println("<BR>Server Version : " + "<b>" 
+ getServerVersion(getServletContext().getServerInfo()) + "</b>");
out.println("<BR>Server Port : " + "<b>"
  + request.getServerPort() + "</b>");
out.println("<BR>Server Run On : " + "<b>"
    + request.getServerName() + "</b>");
out.println(" <hr align=\"left\" width=\"30%\"/>");
out.println("<b>About Servlet</b>");
out.println("<BR>Servlet Information : " + "<b>"
    + getServletInfo() + "</b>");
out.println("<BR>Servlet URL : " + "<b>"
    + request.getRequestURL() + "</b>");
out.println("<BR>Servlet Name :" + "<b>"
    + getServletConfig().getServletName() + "</b>");
out.println("<BR>Servlet Protocol : " + "<b>" 
	+ request.getLocalPort() + "</b>");
out.println("<BR>Servlet Method : " + "<b>"
    + request.getMethod() + "</b>");
out.println("<BR>Servlet Local Address : " + "<b>"
    + request.getLocalAddr() + "</b>");
out.println("<BR>Servlet access : " + "<b>" + count + "</b>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}

@Override
protected void doGet(HttpServletRequest request, 
	HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request,
	HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return "r4r.co.in-infoServlet";
}

/* -- Private Method -- */
//Method handel to display server name
private String getServerName(String serverInfo) {
//Get Server name from serverInfo string
int slashBar = serverInfo.indexOf("/");
if (slashBar == 0) {
return serverInfo;
} else {
return serverInfo.substring(0, slashBar);
}
}

// Method handel to display Server Version
private String getServerVersion(String serverInfo) {
// Get server version from serverInfo string
int slashBar = serverInfo.indexOf('/');
if (slashBar == -1) {
return null;
}
int spaceBar = serverInfo.indexOf(' ', slashBar);
if (spaceBar == -1) {
spaceBar = serverInfo.length();
}
return serverInfo.substring(slashBar + 1, spaceBar);
}
}
Previous Home Next