i want to update a jsp after updating the database what code do i request set attribute
JSP - Form Processing
In this chapter, nosotros will hash out Form Processing in JSP. Y'all must take come up across many situations when y'all need to pass some information from your browser to the web server and ultimately to your backend plan. The browser uses 2 methods to laissez passer this information to the web server. These methods are the GET Method and the Post Method.
The Methods in Course Processing
Let us now talk over the methods in Class Processing.
GET method
The GET method sends the encoded user information appended to the page asking. The folio and the encoded information are separated by the ? graphic symbol equally follows −
http://www.test.com/hi?key1=value1&key2=value2
The GET method is the default method to pass information from the browser to the web server and it produces a long cord that appears in your browser's Location:box. Information technology is recommended that the Become method is better not used. if you have password or other sensitive information to laissez passer to the server.
The GET method has size limitation: only 1024 characters can exist in a request string.
This information is passed using QUERY_STRING header and will be accessible through QUERY_STRING surroundings variable which can be handled using getQueryString() and getParameter() methods of asking object.
POST method
A generally more than reliable method of passing information to a backend program is the Post method.
This method packages the information in exactly the aforementioned style every bit the GET method, but instead of sending it every bit a text string later a ? in the URL it sends it as a split bulletin. This message comes to the backend programme in the form of the standard input which yous tin can parse and use for your processing.
JSP handles this type of requests using getParameter() method to read simple parameters and getInputStream() method to read binary data stream coming from the client.
Reading Course Information using JSP
JSP handles form data parsing automatically using the following methods depending on the situation −
-
getParameter() − Yous call request.getParameter() method to go the value of a grade parameter.
-
getParameterValues() − Call this method if the parameter appears more than once and returns multiple values, for case checkbox.
-
getParameterNames() − Call this method if you want a complete listing of all parameters in the current request.
-
getInputStream() − Call this method to read binary data stream coming from the customer.
Go Method Example Using URL
The post-obit URL will pass two values to HelloForm programme using the Go method.
http://localhost:8080/primary.jsp?first_name=ZARA&last_name=ALI
Below is the main.jsp JSP program to handle input given past spider web browser. We are going to use the getParameter() method which makes information technology very like shooting fish in a barrel to admission the passed information −
<html> <caput> <title>Using Get Method to Read Form Data</championship> </caput> <trunk> <h1>Using Get Method to Read Form Information</h1> <ul> <li><p><b>First Name:</b> <%= asking.getParameter("first_name")%> </p></li> <li><p><b>Last Name:</b> <%= request.getParameter("last_name")%> </p></li> </ul> </trunk> </html>
Now type http://localhost:8080/chief.jsp?first_name=ZARA&last_name=ALI in your browser'southward Location:box. This will generate the post-obit result −
Using GET Method to Read Course Data
-
Starting time Proper name: ZARA
-
Last Proper name: ALI
GET Method Example Using Form
Following is an example that passes two values using the HTML Class and the submit push. Nosotros are going to use the same JSP main.jsp to handle this input.
<html> <body> <form action = "main.jsp" method = "GET"> First Name: <input blazon = "text" name = "first_name"> <br /> Terminal Proper name: <input type = "text" name = "last_name" /> <input blazon = "submit" value = "Submit" /> </form> </body> </html>
Continue this HTML in a file Hello.htm and put information technology in <Tomcat-installation-directory>/webapps/ROOT directory. When you lot would admission http://localhost:8080/Hello.htm , you lot will receive the following output.
< p>Try to enter the First Proper noun and the Concluding Name and then click the submit button to run into the result on your local automobile where tomcat is running. Based on the input provided, it volition generate like outcome every bit mentioned in the in a higher place instance.
POST Method Instance Using Form
Let united states of america practise a little modification in the to a higher place JSP to handle both the GET and the Mail service method. Below is the main.jsp JSP plan to handle the input given past web browser using the Go or the POST methods.
Infact there is no change in the above JSP because the simply mode of passing parameters is changed and no binary data is existence passed to the JSP program. File treatment related concepts will be explained in separate affiliate where we need to read the binary information stream.
<html> <head> <title>Using Become and Mail service Method to Read Form Information</title> </head> <trunk> <center> <h1>Using POST Method to Read Grade Data</h1> <ul> <li><p><b>Showtime Name:</b> <%= request.getParameter("first_name")%> </p></li> <li><p><b>Last Proper noun:</b> <%= request.getParameter("last_name")%> </p></li> </ul> </body> </html>
Following is the content of the Hullo.htm file −
<html> <torso> <grade action = "principal.jsp" method = "POST"> Outset Name: <input blazon = "text" name = "first_name"> <br /> Last Name: <input type = "text" name = "last_name" /> <input type = "submit" value = "Submit" /> </class> </trunk> </html>
Let us now keep main.jsp and hello.htm in <Tomcat-installationdirectory>/webapps/ROOT directory. When yous access http://localhost:8080/Hello.htm , you will receive the following output.
Try to enter the Starting time and the Last Name and and so click the submit push to run into the result on your local machine where tomcat is running.
Based on the input provided, you will receive similar results as in the to a higher place examples.
Passing Checkbox Data to JSP Plan
Checkboxes are used when more one option is required to be selected.
Following is an case HTML code, CheckBox.htm, for a form with two checkboxes.
<html> <torso> <form action = "main.jsp" method = "Mail" target = "_blank"> <input type = "checkbox" name = "maths" checked = "checked" /> Maths <input type = "checkbox" name = "physics" /> Physics <input type = "checkbox" name = "chemistry" checked = "checked" /> Chemical science <input type = "submit" value = "Select Discipline" /> </grade> </trunk> </html>
The above code volition generate the following result −
Following is main.jsp JSP plan to handle the input given by the web browser for the checkbox button.
<html> <caput> <title>Reading Checkbox Data</title> </head> <body> <h1>Reading Checkbox Data</h1> <ul> <li><p><b>Maths Flag:</b> <%= request.getParameter("maths")%> </p></li> <li><p><b>Physics Flag:</b> <%= asking.getParameter("physics")%> </p></li> <li><p><b>Chemistry Flag:</b> <%= request.getParameter("chemical science")%> </p></li> </ul> </torso> </html>
The higher up plan volition generate the following issue −
Reading Checkbox Data
-
Maths Flag :: on
-
Physics Flag:: null
-
Chemistry Flag:: on
Reading All Form Parameters
Following is a generic example which uses getParameterNames() method of HttpServletRequest to read all the available class parameters. This method returns an Enumeration that contains the parameter names in an unspecified lodge.
One time we have an Enumeration, nosotros tin can loop downward the Enumeration in the standard style, using the hasMoreElements() method to decide when to cease and using the nextElement() method to get each parameter proper noun.
<%@ page import = "coffee.io.*,coffee.util.*" %> <html> <head> <title>HTTP Header Asking Example</title> </head> <body> <center> <h2>HTTP Header Request Example</h2> <table width = "100%" border = "one" align = "heart"> <tr bgcolor = "#949494"> <th>Param Proper noun</th> <thursday>Param Value(southward)</th> </tr> <% Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (Cord)paramNames.nextElement(); out.print("<tr><td>" + paramName + "</td>\n"); String paramValue = request.getHeader(paramName); out.println("<td> " + paramValue + "</td></tr>\north"); } %> </table> </center> </body> </html>
Following is the content of the Hello.htm −
<html> <torso> <form action = "main.jsp" method = "POST" target = "_blank"> <input type = "checkbox" proper name = "maths" checked = "checked" /> Maths <input type = "checkbox" proper noun = "physics" /> Physics <input type = "checkbox" proper name = "chemistry" checked = "checked" /> Chem <input blazon = "submit" value = "Select Subject" /> </class> </body> </html>
Now try calling JSP using the higher up Hello.htm; this would generate a result something like as beneath based on the provided input −
Reading All Class Parameters
Param Name | Param Value(southward) |
---|---|
maths | on |
chemical science | on |
Yous can try the above JSP to read whatever other course's data which is having other objects like text box, radio button or dropdown, etc.
Useful Video Courses
Video
Video
Video
Video
Video
Video
Source: https://www.tutorialspoint.com/jsp/jsp_form_processing.htm
0 Response to "i want to update a jsp after updating the database what code do i request set attribute"
Enregistrer un commentaire