Servlets

farrightSoftware and s/w Development

Aug 15, 2012 (4 years and 10 months ago)

229 views

Servlets

A form

The HTML source

<html>


<head>


<title>Chapter 1</title>


</head>


<body><font size="4">


<center>Please enter your name and password then press start<br>


<form method="GET" action="
http://localhost:8084/WebApplication1/GetDemo
" >


Name: <
input

name="uname" value=""
type="text"

size="20"> <br>


Password: <
input

name="userpw" value=""
type="password"

size=10>


<
input

type="submit"

value="Start" > <br>


</form>


</center>


<hr>


</body>

</html>



In Netbeans you can graphically
create forms and elements by
using the HTML palette

(Ctrl
-
Shift
-
8)

A servlet

import java.io.*;

import java.net.*;


import javax.servlet.*;

import javax.servlet.http.*;



public class
GetDemo

extends HttpServlet {




protected void processRequest(HttpServletRequest request,






HttpServletResponse response)


throws ServletException, IOException {


response.setContentType("text/html");


PrintWriter out = response.getWriter();




String username = request.getParameter("uname");


String password = request.getParameter("userpw");






out.println("<HTML>");


out.println("<HEAD><TITLE>GetDemo Output</TITLE></HEAD>");


out.println("<BODY>");


out.println("Hello " + username + "<br>");


out.println("Your password was: " + password + "<br>");


out.println("</BODY>");


out.println("</HTML>");


out.close();



}


protected void
doGet
(HttpServletRequest request, HttpServletResponse response)





throws ServletException, IOException {


processRequest(request, response);


}


protected void
doPost
(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {


processRequest(request, response);


}


public String getServletInfo() { return "Short description"; }

}