Skip to content Skip to sidebar Skip to footer

Creating A Simple Web Page Using Servlet

I want to create a simple web page using servlet. This was one of my exam questions, I did my practical at exam, but it was not working as they expect, However now I want clarify m

Solution 1:

Ajax and Javascript can be used to update the page content without refreshing the whole page.

A way to develop this application is as follows:

There is only one web page required which contains code to divide page into 2 parts.It should display a text box, a text area and a button in second part. And a div (to display comments) in first part. This page should also contain code to handle on click of button. On click of button an Ajax call should be initiated. This ajax call should hit a servlet by passing the entered comment.

The servlet should do the processing of comment and return the comment back as response. The returned comment can be added to the fist part of page by writing javascript code.

Solution 2:

You don't have to complicate this simple servlet program. You need to have one jsp and servlet to do this.

Your requirement is using same page you've to submit the data first and view it.

First create simple jsp page with textbox. You should use JSTL instead of scriptlets, for a simplicity I've given you the scriptlet code.

Your index.jsp looks like this,

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE htmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1"><title>Submit and View Page</title></head><body><!-- This part will enable only after submit your username -->
    <% 
      if(null != request.getParameter("username")){
         out.println("<fieldset><legend>Entered Name</legend>");
         out.println(request.getParameter("username"));
         out.println("</fieldset>");
      }
     %>
    <!-- End of view data --><formaction="HelloServlet"><label>User Name: </label><inputtype='text'name='username'/></br><inputtype='submit'value='Submit'/></form></body></html>

Create respective servlet say HelloServlet,

package com;

import java.io.IOException;

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

publicclassHelloServletextendsHttpServlet {

    privatestaticfinallongserialVersionUID=1L;

    @OverrideprotectedvoiddoGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        // Receive the usernameStringusername= req.getParameter("username");

        // Set it into request object
        req.setAttribute("username", username);

        // Forward it into same index page
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }
}

Configure your deployment descriptor web.xml

<?xml version="1.0" encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"><display-name>Sample_Servlet</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><servlet><servlet-name>HelloServlet</servlet-name><servlet-class>com.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>HelloServlet</servlet-name><url-pattern>/HelloServlet</url-pattern></servlet-mapping></web-app>

Make sure your folder structure look like this, As well you start learning from tutorial.

Servlet Sample

Post a Comment for "Creating A Simple Web Page Using Servlet"