answersLogoWhite

0

What is HttpServletRequest?

Updated: 12/12/2022
User Avatar

Wiki User

15y ago

Best Answer

ServletRequest provides the functionalities to the classes to access the HTTP requests. The servlet container creates an HttpServletRequest object and an HttpServletResponse object, and passes them as arguments to the servlet's service methods such as doGet, doPost, etc.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is HttpServletRequest?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Do can connect database with javascript?

You can't connect directly from a browser to the database, as the browser doesn't have access.Even if you could, you shouldn't: If your Javascript sends SQL statements to the database, there is nothing stopping a random visitor from having fun by changing your SQL to "drop table", and your entire database is gone.What you do is setting up a server side web service (using PHP, Java servlets, Ruby on Rails or whatever) that accepts a URL, converts it to a DB query and returns the result as JSON. The primary thing from a security standpoint is that you never insert a string from the web into the SQL without validating it first.As an example, you could have something like this:--- browser javascript (using jQuery) ---$.getJSON("/productDetail.json?productId=123", function (data) {// do something with the data});--- server (using Java servlet) ---void doProductDetail(HttpServletRequest req, HttpServletResponse res) {int productId = Integer.parseInt(...); // get product ID from query paramString sql = ...; // generate the SQLString json = ...; // execute query, convert result to JSONres.write(json); // send to browser}


What is the relationship between Java and HTML?

Java code can be embedded into HTML and also HTML code can be embedded into java code through Java Server Pages Technology (JSP).eg: Java code inside HTML.Hello! The time is now This is someHelloJavaProgram.jsp , not someHelloJavaProgram.HTML pageeg: HTML code inside Java codeimport javax.servlet.*;import java.io.*;public class HelloServlet extends HttpServlet {public void doGet(HttpServletRequest incoming,HttpServletResponse outgoing)throws ServletException, IOException {outgoing.setContentType("text/HTML");PrintWriter out = outgoing.getWriter();out.println("Hello!");out.println("Hello Servlet World");out.println("");out.close();}}http://www.wellho.net/resources/ex.php4?item=j906/HelloServlet.java


Related questions

Which class a servlet use to receive a request from a client?

HttpServletRequest


What is anatomy of java servlet?

A Servlets skeleton would look like below: /* * servlet name * * servlet description * All other stuff that is part of a Standard Class comment section */ //package declarations //import statements public class ServletName extends HttpServlet { // Instance Variables /** * Code to Initialize the Servlet */ public void init() throws ServletException { // Servlet Initialization Code goes here } /** * The Service Method * Gets invoked for every request submitted to the Servlet * This method is optional. We mostly use doGet, doPost Methods */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Code for the Service Method goes here } /** * Process a GET request * */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Code for the doGet() method goes here } /** * Process a POST request * */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Code for the doPost() method goes here } /** * Process a PUT request * */ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Code for the doPut() method goes here } /** * You can have any number of methods for your processing * here. There is no limit as to the number of methods or * any restrictions on what you can name them. * Since this is all java code, you need to keep them * syntactically correct as per Java Coding Standards. */ /** * Clean-Up */ public void destroy() { // clean up activities before the Servlet is put to death } }


Http servlet response and http request?

HttpServletRequest and HttpServletResponse are the two objects that signify the request received by a servlet and the response sent by it after it is done processing the request. Both the request and response can carry data objects (variables) that can be accessed by the entity receiving the request/response object. They are both an integral part of J2EE Applications.


How do you display the host name and ip address using Servlets?

The host name and the ip address is present in the HttpRequest object. Usually it is passed as an argument to the doXXX methods. public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { // Get client's IP address String ipAddress = req.getRemoteAddr(); // Get client's hostname String hostname = req.getRemoteHost(); }


What use import javaxservlethttpHttpServletRequest in java?

The Http RequestWhen a user hits a URL with a servlet at the other end, the Servlet Container creates an HttpServletRequest object. It passes this object as an argument to the servlet's service methods (doPut(), doGet(), and doPost()). There is a lot of information in this object, including the login details of the user making this request and the name of the HTTP method with which this request was made.


What is difference between GET and POST in servlets in java?

The difference between a GET and a POST is the way data is transferred to a servlet. With a GET, the URL will show each name/value pair on the query string in the URL. For example, if you had a form with a field named 'foo,' and when submitted had a value of 'bar,' the URL might be something like this: http://www.example.com/servlet?foo=bar With a POST, this information is not visible in the URL. Instead, it is transferred in the HTTP headers. As far as the actual servlet is concerned, there is not a great deal of difference when it comes to getting the parameters. Whether you use a GET or a POST, you still use request.getParameter("foo"); to get the value. The method used in the Servlet for processing either a GET or a POST is different too. If you use a GET, the method that is called is doGet(HttpServletRequest, HttpServletResponse). The doGet method is also called if there is no GET or POST data. If you use a POST, the method called is doPost(HttpServletRequest, HttpServletResponse).


What is signature of service method in servlet?

The doXXX MethodsThere are 3 main types of requests that get processed by a Servlet. They are:&bull; Get&bull; Post&bull; PutEach of them have a corresponding doXXX() method in the Servlet class which would be:&bull; doGet&bull; doPost&bull; doPutThese methods are called by the service method in your Servlet.Let us now take a look at a sample Servlet that has these 3 doXXX methods.import java.io.*;import javax.servlet.*;import javax.servlet.http.*;/*** A servlet that has the 3 doXXX Methods** @author Anand*/public class OurSecondServlet extends HttpServlet{// doGet()public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{requestType("GET", response);}// doPost()public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{requestType("POST", response);}// doPut()public void doPut(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{requestType("PUT", response);}public void requestType(String requestType,HttpServletResponse response)throws IOException, ServletException{response.setContentType("text/HTML");PrintWriter out = response.getWriter();out.println("< HTML >");out.println("< head >");out.println("< title >Our Second Servlet" +"< / title >");out.println("< / head >");out.println("< body >");out.println("< h1 >Servlet Request< / h1 >");out.println("The Submitted Request type is : " + requestType);out.println("< / body >");out.println("< / HTML >");}}


How do you write a servlet?

Servlet SkeletonIf I ask you, what are the components of a Java class, you'll happily tell me that, there are first package statements and then imports and then the class declaration. Within the class brackets, we have constructors, instance variables, methods etc. That was easy, wasn't it?The same way, every Servlet has a certain set of components that are mandatory for its well-being. (I just got carried away a bit) Or I must say, for its proper functioning.A Servlets skeleton would look like below:/** servlet name** servlet description* All other stuff that is part of a Standard Class comment section*///package declarations//import statementspublic class ServletName extends HttpServlet {// Instance Variables/*** Code to Initialize the Servlet*/public void init() throws ServletException{// Servlet Initialization Code goes here}/*** The Service Method* Gets invoked for every request submitted to the Servlet* This method is optional. We mostly use doGet, doPost Methods*/protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{// Code for the Service Method goes here}/*** Process a GET request**/protected void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{// Code for the doGet() method goes here}/*** Process a POST request**/protected void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{// Code for the doPost() method goes here}/*** Process a PUT request**/protected void doPut(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{//Code for the doPut() method goes here}/*** You can have any number of methods for your processing* here. There is no limit as to the number of methods or* any restrictions on what you can name them.* Since this is all java code, you need to keep them* syntactically correct as per Java Coding Standards.*//*** Clean-Up*/public void destroy(){// clean up activities before the Servlet is put to death}}The above is what a Servlets skeleton would look like


What is the live example of a servlet?

import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import java.io.PrintWriter; import java.io.IOException; public class OurFirstServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/HTML"); PrintWriter out = response.getWriter(); out.println("&lt; HTML &gt;"); out.println("&lt; head &gt;&lt; title &gt;Servlet Example " + " "); out.println("&lt; body &gt;"); out.println("Not Much code, but this is enough for a Servlet."); out.println(""); out.println(""); } } The above is a simple Servlet. It would display an almost blank HTML page that contains the message we put in "Not Much code, but this is enough for a Servlet."


Write a java program in servlets?

import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import java.io.PrintWriter; import java.io.IOException; public class OurFirstServlet extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/HTML"); PrintWriter out = response.getWriter(); out.println("&lt; HTML &gt;"); out.println("&lt; head &gt;&lt; title &gt;Servlet Example " + " "); out.println("&lt; body &gt;"); out.println("Not Much code, but this is enough for a Servlet."); out.println(""); out.println(""); } } The above is a simple Servlet. It would display an almost blank HTML page that contains the message we put in "Not Much code, but this is enough for a Servlet."


Do can connect database with javascript?

You can't connect directly from a browser to the database, as the browser doesn't have access.Even if you could, you shouldn't: If your Javascript sends SQL statements to the database, there is nothing stopping a random visitor from having fun by changing your SQL to "drop table", and your entire database is gone.What you do is setting up a server side web service (using PHP, Java servlets, Ruby on Rails or whatever) that accepts a URL, converts it to a DB query and returns the result as JSON. The primary thing from a security standpoint is that you never insert a string from the web into the SQL without validating it first.As an example, you could have something like this:--- browser javascript (using jQuery) ---$.getJSON("/productDetail.json?productId=123", function (data) {// do something with the data});--- server (using Java servlet) ---void doProductDetail(HttpServletRequest req, HttpServletResponse res) {int productId = Integer.parseInt(...); // get product ID from query paramString sql = ...; // generate the SQLString json = ...; // execute query, convert result to JSONres.write(json); // send to browser}


Write a servlet to display request header information?

You can create a servlet that overrides the doGet method to access the request headers using the HttpServletRequest object. You can then iterate through the headers and display them as needed. Be sure to set the appropriate content type before writing the response to the HttpServletResponse object.