0

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 my question. My question is there is a webpage and it divide into 2 parts. At the bottom part it should display a text box and a text area to enter comments. And also there is a submit button. When submit button clicks that entered comment should display at the top of the same page. To do this I have created a servlet call Welcome.java and divide it into 2 parts using iframes. Then I created a servlet call test.java and create a textbox and textarea in that servlet.Then create another servlet call text22.java to catch the comment and to display them. But it does not give the expected output.
I attach a screenshot of how this page looks now, I want to display the comment at the top of the page. Please help me to solve this problem.I really appreciate your help...

Welcome.java

 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 neil
     */

public class Welcome extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {


            out.println("<iframe src='test22' name='if1' width='100%' height='400px'>");
            out.println("</iframe>");
            out.println("<iframe src='test' name='if1' width='100%' height='200px'>");
            out.println("</iframe>");


        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    @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 "Short description";
    }// </editor-fold>
}

test.java

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

/**
 *
 * @author neil
 */
public class test extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            out.println("User Name" + "<input type='text' name='username'/>" + "<br>" + "<br>");
            out.println("<textarea name='comment' rows='25' cols='20'>" + "Write your comment");
            out.println("</textarea>");
            out.println("<input type='submit' value='Submit'/>");




        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    @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 "Short description";
    }// </editor-fold>
}

test22.java

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

/**
 *
 * @author neil
 */
public class A extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            ServletContext sc=request.getServletContext();
            String com= (String) sc.getAttribute("text");

            if(null==com){
              com = request.getParameter("comment"); 
            }else{
                com=com+"<br>"+request.getParameter("comment"); 
            }


            out.println(com);

            sc.setAttribute("comment", com);

        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    @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 "Short description";
    }// </editor-fold>
}

Image

neil
  • 21
  • 3
  • When u click on submit where is the control going, how is it going ? – Avinash Reddy Mar 22 '16 at 09:00
  • When click on submit button it should appear at the top of the page, where it now display as null, By the way I'm new to servlet and Jsp, If you help me I really thankfull – neil Mar 22 '16 at 11:53

2 Answers2

0

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 html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-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 -->
    <form action="HelloServlet">
        <label>User Name: </label><input type='text' name='username'/></br>
        <input type='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;

public class HelloServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        // Receive the username
        String username = 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-app xmlns: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

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
  • Thqnk you for your help, but one thing I can not understand is what shold I contain in my index.jsp. – neil Mar 22 '16 at 15:09
0

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.

kamal
  • 84
  • 1
  • 6
  • Thank you,i got that idea, but the problem is I'm not allowing to use Javascript or any other, only can use servlet, jsp – neil Mar 22 '16 at 17:09
  • In that case you can have a index.jsp page which display A form with a text box, a text area and a submit button in second part. In the first part of the page you can iterate a comments list (obtained from session) which you handle in the servlet every time on the submission of the form. – kamal Mar 22 '16 at 17:20