Adv Java - [Servlet: Hidden Field]

♠ Posted by Unknown in at 08:01

Hidden Field in Servlet Example

The HTTP is stateless protocol, means it can not preserve data of current web page while moving backward/forward web pages. The hidden field is a way to State management in servlet example. The hidden field is used to store intermediate data hide while moving from one page to another page.  


Write a servlet program to display Username & Password of a particular client using concept of Hidden field.
                                                    ServletMCA07.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> Concept of Hidden Fields.... </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
            <H1><CENTER> Wel-Come to the World of Hidden Field</CENTER></H1>
            <FORM METHOD = "POST" ACTION = "http://localhost:8080/ServletMCA07">
                        <PRE>
                        Enter User Name : <INPUT TYPE = hidden NAME = "txtName" VALUE = "MCA">
                        Enter Password  : <INPUT TYPE = hidden NAME = "txtPass" VALUE = "MCA">
                        <INPUT TYPE = "SUBMIT" VALUE = "Submit">
                        </PRE>
            </FORM>
</BODY>
</HTML>

                                                     ServletMCA07.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

class  ServletMCA07 extends HttpServlet
{
            public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
            {
                        res.setContentType("text/html");
                        PrintWriter out = res.getWriter();
                        String s1 = req.getParameter("txtName");
                        String s2 = req.getParameter("txtPass");
                        out.println("<HTML><HEAD><TITLE>Hidden Field Output</TITLE></HEAD>");
                        out.println("<BODY> My Name is : " + s1 + "<BR>My Password is : " + s2 + "</BODY></HTML>");
                        out.close();
            }
}

0 comments:

Post a Comment