SpringBoot-Basic web page writing

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My home page</title>
</head>
<body>
    <!--
        <h1>-<h6>:title,The title is characterized by a line of its own,Decrease the font size of the title
        <center>:This tag is no longer recommended(did not study css temporary use),The role is to center the components it contains on the page
        <input>:input component,This component is used to get user input on the page
                Components can be represented in different ways,have attributes type Decide.
                Common values:
                type="text" : text box
                type="password" : password box
                type="checkbox" : checkbox
                type="radio" : Single box
                type="button" : button
                type="submit" : form submit button
                ...

         <a>:Hyperlink,The text between the tags is the prompt information on the hyperlink,while attributes href Used to specify the address to jump to after clicking the hyperlink.

         <br>:newline

         <table>Label:sheet. Attributes border Used to specify borders.
                    <table>the label contains<tr>Labels are used to denote lines
                    <tr>the label contains<td>Labels are used to represent columns
                    <td>Common attributes in tags:
                    align:its way. left Align left,right Align right,center in the play
                    colspan:Merge columns across columns, merging is merging columns from left to right
                    rowspan:Merge columns across rows, merging is merging columns from top to bottom

         <img>Label:picture. Attributes src path to specify image

         exist HTML In the page, we often introduce a resource of the server by specifying a path. When specifying the path, we usually use the"/"as a start
         And here's"/"It can be understood that the definition is that in the server static The location of the directory.
         Essentially it expresses the very beginning of the "abstract path" in the address bar./"Location.
                              v
         http://localhost:8088/index.html
         E.g:exist index.html Introduce an image to the page:
         <img src="/logo.png">
         At this point, when the browser understands the actual location of the path, the corresponding:
         http://localhost:8088/logo.png

    -->
    <center>
        <!--<h1>Baidu</h1>-->
        <img src="/logo.png"><br>
        <input type="text" size="32">
        <input type="button" value="Baidu" onclick="alert('click your sister!')">
        <br>
        <a href="/classtable.html">Class Schedule</a>
        <a href="/reg.html">User registration</a>
        <br>
        <a href="http://www.taobao.com">Taobao</a>
        <br>
        <table border="1">
            <tr>
                <td rowspan="2">first row</td>
                <td>the second list</td>
                <td colspan="2" align="center">third column</td>
            </tr>
            <tr>
                <td>the second list</td>
                <td>third column</td>
                <td>fourth column</td>
            </tr>
        </table>

    </center>
</body>
</html>

 

<!--
    <h1>-<h6>: Title, the title is characterized by an exclusive line, and the font size of the title decreases
    <center>: This tag is no longer recommended (temporarily used before learning css), the function is to center the components it contains on the page
    <input>: Input component, this component is used to get user input on the page
            Components can be represented in different ways, depending on the attribute type.
            Common values:
            type="text" : text box
            type="password" : password box
            type="checkbox" : checkbox
            type="radio" : radio button
            type="button" : button
            type="submit" : form submit button
            ...

     <a>: Hyperlink, the text between the tags is the prompt information on the hyperlink, and the attribute href is used to specify the jump address after clicking the hyperlink.

     <br>: newline

     <table> tag: table. The property border is used to specify the border.
                <table> tags contain <tr> tags to indicate rows
                <tr> tags contain <td> tags to indicate columns
                Common attributes in <td> tags:
                align: Align the way.  Left is aligned to the left, right is aligned to the right, and center is aligned in the play
                colspan: merge columns across columns, merging is merging columns from left to right
                rowspan: Merge columns across rows, merging is merging columns from top to bottom

     <img> tag: image. The attribute src is used to specify the path of the image

     In HTML pages, we often introduce a resource from the server by specifying a path. When specifying a path, we usually start with "/"
     The "/" here can be understood as defining the location of the static directory in the server.
     Essentially it expresses the initial "/" position in the abstract path in the address bar.
                          v
     http://localhost:8088/index.html
     For example: introduce an image on the index.html page:
     <img src="/logo.png">
     At this point, when the browser understands the actual location of the path, it corresponds to:
     http://localhost:8088/logo.png

-->

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>User registration</title>
</head>
<body>
<center>
    <h1>User registration</h1>
    <!--
               form form
               form A form is a component used to submit the information entered by the user on the page to the server.
               form The form should contain several input components.
               Notice:Only include early form Only the information entered by the user in the input component in the middle of the label will be submitted to the server!!

               form There are two important properties on:
               action:Used to specify the path for form submission, which needs to be processed by the server
               method:Form submission form, there are two options:
                      GET:Submit in the form of the address bar, the form data will be spliced ​​to the address bar URL pass in
                      POST:The form data will be included in the message body of the request to be submitted
                      Should be used when form data contains user private information or when attachments are uploaded POST. 

                      method When the attribute is not specified, the default is GET Request submission.
            -->
    <!--
        Since the current registration page is requested, the path on the browser address bar is:
        http://localhost:8080/reg.html
        So on the current page we define the form<form action="/regUser">, the actual request path after the form is submitted is:
        http://localhost:8080/regUser

        After submitting, our server gets the URL abstract path(/regUser)after the first"/"locate first static Does the directory have
        The file, if any, is sent directly to the browser

        for form get What the browser address bar looks like after the form is submitted
        http://localhost:8080/regUser?username=fancq&password=123456&nickname=chuanqi&age=22

        in the form<form action="/regUser" method="get"> method value of get When the expression submits form data in the form of an address bar.
        At this point the form data will be spliced ​​to URL in the abstract path. The request part with the abstract path starts with"?"separate form
        abstract path format:
        request path(action specified content)?Parameters section
        /regUser?username=fancq&password=123456&nickname=chuanqi&age=22
        Format of the parameter section:
        parameter name 1=parameter value 1&parameter name 2=parameter value 2&...
        The parameter name is an input box in the form name the value specified by the property
        The parameter value is what the user entered in the input box in the form

     -->
    <form action="/regUser" method="get">
        <table border="1">
            <tr>
                <td>username</td>
                <td><input name="username" type="text"></td>
            </tr>
            <tr>
                <td>password</td>
                <td><input name="password" type="password"></td>
            </tr>
            <tr>
                <td>Nick name</td>
                <td><input name="nickname" type="text"></td>
            </tr>
            <tr>
                <td>age</td>
                <td><input name="age" type="text"></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="register">
                </td>
            </tr>
        </table>
    </form>
</center>
</body>
</html>

<!--
           form form
           A form is a component used to submit the information entered by the user on the page to the server.
           A form should contain several input components.
           Note: Only the information entered by the user in the input component in the middle of the early form tag will be submitted to the server!!

           There are two important attributes on the form:
           action: used to specify the path for form submission, which needs to be processed by the server
           method: The form of form submission, there are two options:
                  GET: Submit in the form of the address bar, the form data will be spliced ​​into the URL of the address bar and passed
                  POST: The form data will be submitted in the message body of the request
                  POST should be used when form data contains user private information or when attachments are uploaded.

                  When the method attribute is not specified, the default is GET request submission.
        -->
<!--
    Since the current registration page is requested, the path on the browser's address bar is:
    http://localhost:8080/reg.html
    Therefore, when we define the form <form action="/regUser"> on the current page, the actual request path after the form is submitted is:
    http://localhost:8080/regUser

    After submitting, after our server gets the abstract path (/regUser) in the URL, the first "/" first locates whether there is any file in the static directory.
    The file, if any, is sent directly to the browser

    After the form is submitted in the get form, the browser address bar looks like
    http://localhost:8080/regUser?username=fancq&password=123456&nickname=chuanqi&age=22

    When the value of the <form action="/regUser" method="get"> method in the form is get, it expresses that the form data is submitted in the form of the address bar.
    At this point the form data will be spliced ​​into the abstract path of the URL. Separated from the request part of the abstract path with a "?"
    Abstract path format:
    Request path (content specified by action)? Parameters part
    /regUser?username=fancq&password=123456&nickname=chuanqi&age=22
    Format of the parameter section:
    parameter name 1=parameter value 1&parameter name 2=parameter value 2&...
    The parameter name is the value specified by the name attribute of an input box in the form
    The parameter value is what the user entered in the input box in the form

 -->

 

package com.tedu.springboot2206.controller;

import com.tedu.springboot2206.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

@Controller
public class UserController {
    //Represents the directory users where all user information is stored
    private static File userDir;

    static{
        userDir = new File("./users");
        if(!userDir.exists()){
           userDir.mkdirs();
        }
    }

    /**
     *
     * @param request   The request object, which encapsulates all the content sent by the browser
     * @param response  The response object, which encapsulates the content we are about to reply to the browser
     */
    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("Start processing registrations! ! ! ! ! ! ! ! ! ! ! ! ! !");
        /*
            The general process of processing registration
            1:Get the registration information entered by the user on the registration page (obtain the form data submitted by the browser through the request object)
            2:Process registration
            3:Set the response object and send the processing result back to the browser
         */
        //1 Get the form information on the registration page
        /*
            HttpServletRequest Important method:
            String getParameter(String name)
            Get the value of a parameter passed by the browser
            The parameter name passed in here corresponds to the name of the page form input box (the value corresponding to the name attribute)
         */
        String username = request.getParameter("username");//get username
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+ageStr);
        /*
            String username = request.getParameter("username");
            Function: Get the value corresponding to the parameter username passed by the browser
            The returned string may have 2 special cases:
            1:Returns an empty string, indicating that the user has not entered anything on the input box
            2:Returns null, indicating that the parameter is not passed
         */
        if(username==null||username.isEmpty()||password==null||password.isEmpty()||
          nickname==null||nickname.isEmpty()||ageStr==null||ageStr.isEmpty()||
          !ageStr.matches("[0-9]+")){
            try {
                response.sendRedirect("/reg_info_error.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }




        int age = Integer.parseInt(ageStr);//Convert age to int value
        /*
            2
            The registered user information is represented in the form of a User object and serialized to a file for saving

            In the future, all files that save user information will be placed in the users directory, and the name format of each file that saves users: username.obj

         */
        User user = new User(username,password,nickname,age);
        /*
            File overloaded constructor
            File(File parent,String child)
            The File object expresses the child item child in the directory represented by parent
         */
        //The file xxx.obj in the userDir (this object represents the users directory under the current project directory) directory (xxx is the current registered user name)
        //          new File(userDir,"fancq.obj")
        File file = new File(userDir,username+".obj");

        //If the file exists then this is a duplicate user
        if(file.exists()){
            try {
                response.sendRedirect("/have_user.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }



        try(
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(user);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //3
        try {
            /*
                Let the browser redirect to the specified path to view the processing result page
                Here the path "/reg_success.html" is sent to the browser for it to understand.
                So the browser still treats the "/" as the first "/" in the abstract path
                Equivalent to the browser will request according to the path:
                http://localhost:8080/reg_success.html
             */
            response.sendRedirect("/reg_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
//represents the directory users where all user information is saved
/*
    The general process of processing registration
    1: Get the registration information entered by the user on the registration page (obtain the form data submitted by the browser through the request object)
    2: Process registration
    3: Set the response object and return the processing result to the browser
 */
//1 Get the form information on the registration page
/*
    Important methods of HttpServletRequest:
    String getParameter(String name)
    Get the value of a parameter passed by the browser
    The parameter name passed in here corresponds to the name of the page form input box (the value corresponding to the name attribute)
 */
/*
    String username = request.getParameter("username");
    Function: Get the value corresponding to the parameter username passed by the browser
    The returned string may have 2 special cases:
    1: Returns an empty string, indicating that the user has not entered anything in the input box
    2: Return null, indicating that the parameter is not passed
 */
/*
    2
    The registered user information is represented in the form of a User object and serialized to a file for saving

    In the future, all files that save user information will be placed in the users directory, and the name format of each file that saves users: username.obj

 */
/*
    File's overloaded constructor
    File(File parent,String child)
    The File object expresses the child item child in the directory represented by parent
 */
//File xxx.obj in the userDir (this object expresses the users directory under the current project directory) directory (xxx is the current registered user name)
//          new File(userDir,"fancq.obj")
//If the file exists then this is a duplicate user
/*
    Let the browser redirect to the specified path to view the processing result page
    Here the path "/reg_success.html" is sent to the browser for it to understand.
    So the browser still treats the "/" as the first "/" in the abstract path
    Equivalent to the browser will request according to the path:
    http://localhost:8080/reg_success.html
 */

Tags: Java Spring Boot servlet

Posted by Ellen67 on Tue, 13 Sep 2022 02:04:00 +0930