I recently watched Android development videos, and the integrated development tool used in it is Eclipse. Using Eclipse can quickly write web projects, and the Android Studio I used has castrated the functions of building other projects because of its professionalism. Therefore, I couldn't bear the embarrassment of being able to listen to the teacher and not actually operate, so I chose to use IntelliJ IDEA instead of Eclipse to simulate network requests. The following is a simple network request implementation to introduce the use of IntelliJ IDEA.
The first thing is to download the IntelliJ IDEA integration tool. This Google/Baidu can be easily obtained.
Next, configure the Tomcat server. Take the Mac computer as an example, refer to: Installation and Configuration of the tomcat server on Mac.
Then open IntelliJ IDEA, select the Java Enterprise project type on the right, select the newly installed Tomcat server, and check the Web Application option.
New project
Click next and enter the custom project name demo:
project
Then we can see the full picture of the new project:
project
At this point, a framework for web application engineering has been completed. However, in order to successfully deploy to the Tomcat server, we also need to add the object Servlet that handles the service. Click the src folder and add Servlet:
servlet
You can see the default generated doGet and doPost methods in the Servlet class:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html"); response.getWriter().print("post request received"); String username = request.getParameter("username"); String pwd = request.getParameter("password"); if("admin".equals(username) && "abc123".equals(pwd)) { response.sendRedirect("/2.html"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8");//Set response.setContentType("text/html"); String username = request.getParameter("username"); String pwd = request.getParameter("password"); if("admin".equals(username) && "abc123".equals(pwd)) { response.sendRedirect("/2.html"); } }To use the newly created Servlet class, you also need to configure it in web.xml:
<web-app ...> <servlet> <servlet-name>Servlet</servlet-name> <servlet-class>demo.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet</servlet-name> <url-pattern>/demo</url-pattern> </servlet-mapping></web-app>
The servlet-mapping tag sets the path to external access.
Then add the front-end page file in the web directory, such as naming 1.html as the starting page and 2.html as the jump result page.
page
Edit the page layout in 1.html, set the head label, and add form form in the body label.
<!DOCTYPE html><html lang="en"><head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" > <title>MyFirst</title> <script type="text/javascript"> </script></head><body><h1>Login page (get)</h1><form action="/demo" method="get"> <table> <tr> <td> Username: </td> <td> <input type="text" name="username"> </td> </tr> <tr> <td> Password: </td> <td> <input type="text" name="password" type="hidden"> </td> </tr> <tr> <td colspan="2" style="align-items: center"> <input type="submit" value="Login"> </td> </tr> </table></form><h1>Login page (post)</h1><form action="/demo" method="post"> <table> <tr> <td> Username: </td> <td> <input type="text" name="username"> </td> </tr> <tr> <td> Password: </td> <td> <input type="text" name="password" type="hidden"> </td> </tr> <tr> <td colspan="2"> <input type="submit" value="Login"> </td> </tr> </table></form></body></html>
2.html to edit the page:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <h1 style="color: red"> Login successfully! ! ! </h1></body></html>
Finally, click Debug to run and deploy it to your Tomcat server:
Debug
Finally, enter the URL in the browser: http://localhost:8080/1.html to access the website we deployed.
website
Open Chrome's developer tools and you can see the details of sending requests:
Send a request
Completed!
The process is very simple. You can use IDEA to learn basic knowledge of back-end development in the future. For example, you can obtain submitted files on the back-end, jump to successful requests, inform the client when the request fails, etc., and you can simulate it. More knowledge points are waiting for you to discover.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.