sample servlet that have written using netbeans.i have use only the doPost method.normally in netbeans they forward the request to the processRequest() method in the servlet.so these things can be done in that method olso.no difference.
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NewServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//to get a parameter
String s= request.getParameter("what ever parameter sent from the previouse page");
//to get multiple parameters
String []s1= request.getParameterValues("para name");
//geting a header
String header= request.getHeader("name");
//get cookie info
Cookie []c= request.getCookies();
//to get session information
HttpSession session= request.getSession();
//to get clients port
getRemotePort();
}
//have to implement a method to get clients port
private void getRemotePort() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
Wednesday, March 19, 2008
Request & Response
now we have come to know what are requests and responses.these derive from two interfaces
the request comes from
now lets consider about the response.there are two interfaces which will be generating the response to the client
ServletResponse
the request comes from
- ServletRequest
- HttpServletRequest
- ServletRequest
- getAttribute(String)
- getLocalPort()
- getParameter()
- getParameterNames()
- HttpServletRequest
- getCookies()
- getHeader(String)
- getSession()
- getMethod()
now lets consider about the response.there are two interfaces which will be generating the response to the client
- ServletResponse
- HttpServletResponse
ServletResponse
- getWriter()
- setContentType()
- addHeader()
- addCookie()
- sendError()
Monday, March 17, 2008
Servlet basics
when we consider about the basics of a servlet we have to find the origin of it.
the Servlet interface comes here.which is javax.servlet.Servelt.we have some usefull method definitions there.some of them are
the Servlet interface comes here.which is javax.servlet.Servelt.we have some usefull method definitions there.some of them are
- service(ServletRequest, ServletResponse)
- init(ServletConfig)
- destroy()
- getInitParameter()
- getInitParameterNames()
- service(HttpServletRequest,HttpServletResponse)
- doGet(HttpServletRequest,HttpServletResponse)
- doPost(HttpServletRequest,HttpServletResponse
servlet config and servlet context
the ServletConfig object contain the deploy time information of a servlet and the ServletContext will be accessed through this object.and per servlet there will be only one ServletConfig object.the servletContext is used to access web application parameters.and per web application there will be one ServletContext.the doGet() and the doPost()
post is more secured and it has a body where the get does not have.we can use a limited number of parameters with the get.post cannot be bookmarked and is used to send data to process.get is used to get some resourse.
Sunday, March 16, 2008
The servlet life cycle
before learning about servlets lets discuss about the life cycle of the servlet.
when a client request something the request is directed to the container.the container makes two objects,which are
the thread comes in to action in one of the two ways mentioned below.
then the service() method redirect the request to a relevant method which may be doGet() or doPost() according to the request. we will learn about these methods in future.
after executing one of these methods the generated response is directed to the browser by the container.and the threads work is also over.
so the thread goes to dead state if it is a newly created one and it will go back to pool if it was taken form the thread pool.
now there is no need of the request and response objects.so they are eligible for the garbage collector to be collected.
and when the servlet is no longer needed the destroy() method of the servlet is called and the servlet moves to the dead state.this is also called only once.
**for every request we make a new thread comes to the action.**
when a client request something the request is directed to the container.the container makes two objects,which are
- request object
- response object
the thread comes in to action in one of the two ways mentioned below.
- a thread pool provides one of the threads it have
- a new thread is created(new Thread();)
then the service() method redirect the request to a relevant method which may be doGet() or doPost() according to the request. we will learn about these methods in future.
after executing one of these methods the generated response is directed to the browser by the container.and the threads work is also over.
so the thread goes to dead state if it is a newly created one and it will go back to pool if it was taken form the thread pool.
now there is no need of the request and response objects.so they are eligible for the garbage collector to be collected.
and when the servlet is no longer needed the destroy() method of the servlet is called and the servlet moves to the dead state.this is also called only once.
**for every request we make a new thread comes to the action.**
Thursday, March 13, 2008
The difference between a web container and a web server
web container is also called as the application server. as you know tomcat is a popular web container.is there a difference between a web server and a web container?
actually yes.
a web server usually serves html pages and images.they are static things unless they contain java script in them.
but when using jsp's and servlets we must use an application server to support them.because it is a software that supports multiple operations in a dynamic environment.
when we request something through our browser(eg:button click) it comes to the web server abd the server redirect that request to the container.
the container manages the whole other things.such as threading,security,communication and life cycle management and so on.
when the request is completed the container forwards the generated response to the web server.then the server displays through the browser.we will learn more in the Servlet life cycle.
actually yes.
a web server usually serves html pages and images.they are static things unless they contain java script in them.
but when using jsp's and servlets we must use an application server to support them.because it is a software that supports multiple operations in a dynamic environment.
when we request something through our browser(eg:button click) it comes to the web server abd the server redirect that request to the container.
the container manages the whole other things.such as threading,security,communication and life cycle management and so on.
when the request is completed the container forwards the generated response to the web server.then the server displays through the browser.we will learn more in the Servlet life cycle.
Subscribe to:
Posts (Atom)