0 Replies - 548 Views - Last Post: 21 March 2016 - 09:11 PM

#1 neil p   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-June 15

create a simple web page using servlet

Posted 21 March 2016 - 09:11 PM

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>
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1