what is servlet
Before knowing servlet you must know what is server and client mean.
SERVER : Server is nothing only where your data and application program are store ;
CLIENT : Clients are those who intend to access your data or any related request that they want... so what a client a browser is client because they request to your server(where ur data store) to access those particular thing mean a page( main.jsp, heeloo.java etc).
now need to know about HTML and JAVASCRIPT these are clint side programing languse
because it is written for browser like when you open a browser need to write somr input to send < input type text name ="any name " value ="any values > mens send etc
SERVLET : It is server side java program means what the program written in java languge and store in your machine where your data and apliction store too : so why we write this java program (servlet) to accept the request by brobse and give some response related to that request : so how it does these thing is a question ?
so must know some thing like HTTPrequest and HTTPresponse these two r protcol for transfer data from one node to other if any node want data then need to call httprequest and when need to send data then HTTPreponse is call and also remember both are stateless protocol mens one time they acecess but not remember in other times etc
Servlet is Generic servlet and child of generic servlet is called httpServlet
means Generic servlet have able to transfer FTP , TELNET etc these are statefull protocl they able to maintain their state And httpServlet accept only http protocol
hence when we create java program for access request by browser need to extend generic servlet or httpservlet mostly extend httpservlet and u may call it as servlet program ,
actually servlet is big topic need to know his life time 3 method (init() service(), distroy()) etc
but for basic only need 5b things
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
HttpServletRequest request here request is object HttpServletRequest
HttpServletResponse response here response is object HttpServletResponse
we use thse two object to access and send data
response.setContentType("text/html"); by default content type is text
PrintWriter out = response.getWriter(); out is object created by us mense send response return to browser by these object and all thing senT in html code because browser can understand and create some use full out put
requesr.getParameter(any thing here that name in ur clinte side browser of input )
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWWW extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("< ! DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n" +
"< HTML>\n" +
"< HEAD>< TITLE>Hello WWW TITLE> HEAD>\n" +
"< BODY>\n" +
"< H1>Hello WWW H1>\n" +
" BODY> HTML>");
}
}
advertisements