JSP simple exercise-Get form data with Servlet, jspservlet

Source: Internet
Author: User

JSP simple exercise-Get form data with Servlet, jspservlet

// JavaBean code package servlet; import java. io. *; import javax. servlet. *; import javax. servlet. http. *; public class AcceptUserRegist extends HttpServlet {public String codeToString (String str) // function for processing Chinese strings {String s = str; try {byte tempB [] = s. getBytes ("ISO-8859-1"); s = new String (tempB); return s;} catch (Exception e) {return s ;}} public void init (ServletConfig config) throws ServletException {super. init (config);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// set mimeresponse. setContentType ("text/html; charset = GB2312"); PrintWriter out = response. getWriter (); out. println ("

<! -- Servletform. jsp --> <% @ page contentType = "text/html; charset = gb2312" %> <script language = "javascript"> function on_submit () // verify the validity of the data {if (form1.username. value = "") {alert ("the user name cannot be blank. Enter the user name! "); Form1.username. focus (); return false;} if (form1.userpassword. value =" ") {alert (" the user password cannot be blank. Enter the password! "); Form1.userpassword. focus (); return false;} if (form1.reuserpassword. value =" ") {alert (" the password cannot be blank. Please enter the password! "); Form1.reuserpassword. focus (); return false;} if (form1.userpassword. value! = Form1.reuserpassword. value) {alert ("the password is different from the Confirmed password"); form1.userpassword. focus (); return false;} if (form1.email. value. length! = 0) {for (I = 0; I <form1.email. value. length; I ++) {if (form1.email. value. charAt (I) = '@') {break;} if (I = form1.email. value. length) {alert ("invalid email address! "); Form1.email. focus (); return false ;}} else {alert (" enter email! "); Form1.email. focus (); return false ;}} </script> In addition, add the following configuration to the <web-app> and </web-app> of the web. xml file:

  <servlet>     <servlet-name>acceptUserRegist</servlet-name>     <servlet-class>servlet.AcceptUserRegist</servlet-class>  </servlet>  <servlet-mapping>     <servlet-name>acceptUserRegist</servlet-name>     <url-pattern>/acceptUserRegist</url-pattern>  </servlet-mapping>
Enter the URL: localhost: 8080/jspdemo/servletform. jsp

Enter information ~ As follows:

After submission, the following page is displayed:




How does servlet obtain form data?

(1)
Yes. Servlet can only read form data in three ways.

Read a single value: getParameter
Read multiple values: getParameterValues
Search for parameter names: getParameterNames and getParameterMap
(2)
Currently, using the JavaBean Component Architecture in JSP greatly simplifies the process of reading request parameters, extracting corresponding values, and storing the results to java objects. The JSP syntax (jsp: setProperty calls property = "*") can be used to complete bean filling.
The work Servlet that JSP can do should also be done. After all, JSP is essentially a Servlet. Every JSP page must be converted to a Servlet, and Servlet is also running during the request. However, the Servlet specification does not provide such a function: the code for completing the Property = "*" JSP process is not exposed by a standard API.
Fortunately, the Jakara general package of the Apache Software Foundation has been widely used, the class we provide is a utility that we can easily build to automatically associate Request Parameters with Bean properties.
That is, BeanUtilities. populateBean
To download the Jakarta universal package, go to the Apache official website.

How to obtain JSP table content in Java Servlet

Can you put <% = titles [row] %> in the text box?
<Input type = "text" name = "title" value = "<% = titles [row] %>" readonly = "readonly"/>
Request. getParameter ("title ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.