Servlets

What is SingleThreadModel in Servlets?

1. What is SingleThreadModel? It is an interface from javax.servlet package used with Servlets. It is marker interface having no methods. Few servlets may require this to implement. 2. Are Servlets multithreaded? Yes, Servlets are multithreaded being Java supports multithreading. 3. Explain how Servlets are multithreaded? First time when a…

getContentLength() Method Example

This method is defined in ServletRequest interface from javax.servlet package and inherited by HttpServletRequest. With this method, the servlet programmer can know the length of data (through FORM fields like user name etc.) sent by the client. Or to say, the length (in bytes) of query string. What is query…

getQueryString() Method Example

This method is defined in HttpServletRequest interface from javax.servlet.http package. With this method, the servlet programmer can know the data (through FORM fields like user name etc.) sent by the client. What is query string in servlets? It is the string containing the name of FORM fields and the data…

getProtocol() vs getScheme()

Both are defined in ServletRequest interface. Using these methods, the servlet programmer can know the protocol used by the client to call the servlet. They differ slightly in the output. getProtocol() returns the protocol with version and getScheme() returns protocol without version. See the output screen at the end. Let…

getRemoteHost() Method Example

This method is defined in ServletRequest interface from javax.servlet package inherited by HttpServletRequest. With this method, the servlet programmer can know the name of the client from which the request came. Let us see what Java API says about this method. java.lang.String getRemoteHost(): Returns the fully qualified name of the…

PrintWriter Vs ServletOutputStream

Earlier we have seen the ways of receiving data from client. Now let us do with response. The response object functionality is to send data to client of what client is interested. For this, response uses IO Streams. There are two streams involved depending on the nature of data like…

Multiple Submit Buttons in HTML Form Servlets

All the HTML forms so far used contains only one submit button. But yet many times, it will be necessary to have multiple submit buttons in a single HTML form. User is at choice to click any one submit button. Now the question is how the Servlet knows from which…

Feedback Form Servlet Example

All the time we are accustomed to read client data with getParameter() method, most probably from HTML text fields. But, HTML FORM tag includes many GUI components like radio buttons, SELECT drop down lists, text areas etc. This Client HTML comes with rarely used FORM fields. To do with, a…

Servlet Read HTML File Contents Example

Earlier we did a program where client requested for a text file; but the case now is with HTML file. Now it is HTML file with Servlet read html file. If the user sends a request for HTML file, when it comes to the browser, it is interpreted and output…

Servlet Read text file and Return contents Example

Sometimes, the client may ask the Servlet to send the file contents of a file existing on the server. It is not file download. Here, the Servlet reads manually line-by-line and send each line separately to client. Read Servlet Read text file. Example on Servlet Read text file Client HTML…

Servlet WAR File Weblogic Creation Deployment

Servlet WAR File Weblogic Creation, Deployment and Execution is shown step-by-step for a Beginner. With the release of the Java Servlet Specification 2.2, the concept of a Web application was introduced. According to this specification, a "Web Application is a collection of servlets, html pages, classes, and other resources that…

Send Image Servlet to Client Example

So far all the earlier examples, we used to send text or HTML messages (text/html) to client. Now-a-days, images are also playing important role in client/server communication. For example, you go to Bank and give a self cheque to draw some money. Immediately, the Bank clerk checks your face to…

getHeaderNames() getHeader() Example

Retrieve Header Information with getHeaderNames() getHeader() When a client clicks a hyper link or a submit button, we know that the data entered by the user in the Form fields is sent to server and is the default behavior of submit button. Ofcourse, right. But along with a lot of…

getRemoteAddr() Method Example

getRemoteAddr() method is defined in ServletRequest interface from javax.servlet package inherited by HttpServletRequest. With this method, the servlet programmer can know the IP address of the client from which the request came. Let us see what Java API says about this method. String getRemoteAddr(): Returns the Internet Protocol (IP) address…

Servlet Life Cycle Example with JDBC

After learning what Servlet life cycle is and the methods involved, let us go for an example involving the life cycle methods. I take the opportunity to connect to database and read table records using life cycle methods. Or to say, illustration of life cycle methods and learning JDBC through…

Servlet Life Cycle Tutorial with Methods

This Servlet Life Cycle Tutorial gives the meaning of life cycle, the methods involved, their importance and how and where to use in coding 1. What is Servlet Life Cycle? Definition: Different states in which a Servlet exists between its object creation and object garbage collection is known as life…

Java Servlet Cookie API Methods

Servlet Cookie: Before going into the API, first let us know what for Servlet Cookie is? For better understanding the subject is discussed in Question/Answer format. 1. What is Cookie? For us, Cookie is a class from javax.servlet.http package. But in the open world, a Cookie represents a sweet Biscuit…

Servlet Cookies Simple Shopping Cart Example

Note: You can see the JESSIONID (Session ID) in the output screens at the end. Note: For better understanding, the subject is discussed in Question/Answer format. Note: Before going into this example, it is advised to go through the Cookie API methods and their explanation because these methods are used…