Advanced Java Development notes in class (D)

Development Log function

In the last article, we completed the building (refer to Advanced Java Development class notes (c)) web services, can now start preparing to develop code.
First, in the main folder, create a javafolder used to store our code, this folder is set to Sources Root:
Here Insert Picture Description
Secondly, we are in javathis folder, create a controllerpackage, create a new LoginControllerclass:
Here Insert Picture Description
At this point we you can start preparing to develop a login function. First of all, we first configure the Servlet in web.xml:

  <servlet>
    <servlet-name>loginServlet</servlet-name>
    <servlet-class>cn.edu.mju.project2.controller.LoginController</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>

Which <Servlet-name>is to be registered Servlet name <Servlet-class>is the address to be registered Servlet class, and to bring the package path. <Servlet-mapping>We are used to configure the access path components registered, including two nodes inside, is a <Servlet-name>previous written Servletkeeping the other is <url-pattern>to configure the components of the access path.
If you think the above method is too much trouble, you can directly LoginControllerconfigure it:

@WebServlet("/login")

Here Insert Picture Description
At this point WebServlet there was an error, since no import jar package is due, we click Import classcan be.
Once configured WebServlet, due LoginControllerto inheritance HttpServlet, so we have to be rewritten doGet and doPost:
doGet: Here Insert Picture Description
doPost:
Here Insert Picture Description
Tip: If you want to display Chinese on the web interface, you need to add the following two codes in the code ( here, for example by doGet code):
Here Insert Picture Description
Let's look at the current results, run the program, open the browser, enter the previously set a good URL:
Here Insert Picture Description
Finally, do not forget the code will be submitted to the local development of the warehouse, and push to the remote repository.

Released seven original articles · won praise 0 · Views 197

Guess you like

Origin blog.csdn.net/Vinseny/article/details/105214594