当前位置:网站首页>Simple interaction between server and client

Simple interaction between server and client

2022-08-11 09:25:00 It's classmate Xiaoming.

Simple interaction between server and client


语言的跨平台


c/c++的跨平台原理

对于不同的操作系统,A different compilation is required,Generate executable files in different formats.

img

java的跨平台原理

对于不同的操作系统,只需编译一次.The generated bytecode file after compilation,对于操作系统来说,is not an executable file.如果要执行,A software needs to be installed on a different operating systemJVM(java虚拟机),And need to open this program.当.javaThe file is generated after compiling.classWhen the file needs to be executed,is needed to start thisJVM程序,然后使得JVM来加载这个.class文件(Parse and translate this file into machine instructions).

补充:JDK中是带有JRE的(Java Runtime Environment),JRE中包含着JVM.So download itJDK就可以运行.class字节码文件了.

img

js的跨平台原理

对于不同的操作系统,不需要编译,Just need to open different browsers in different operating systems,然后浏览器对js文件进行解释,Then make it executable(机器指令).





客户端vs服务器

端口

Both the server and the client are essentially computers,It's just that the client may require a higher graphics card, etc,What the server is asking for is changed memory.Each server will have multiple ports,After the port will correspond to the server software,When the client sends data to the server's port,Server software listens for data and parses and executes code.If the server wants to transmit data back to the client,It is also transmitted through the port.

所以,If you want to build a server environment,One port is required,Then build server software for this port.

部署项目

A client may send multiple requests to the same port,The server software is then asked to create different projects(Develop different backends,可以使用javaCode writing and creation),It is up to the server software to identify different requests and respond.If the server software is too stressed,You can create multiple ports and create server software,to share other projects.If the server is under too much pressure,Then make another machine.

所以,If the selected language is java,The server software can then be deployed on the portTomcat,Used to run developer-createdJava代码.又由于Tomcat软件是由java编写,So if you need to run this software,那么就需要有JVM来加载(If sent by the clientjavaThe code's request needs to be executed,需要先启动JVM,然后再启动Tomcatto find and execute the developer accordinglyjava代码,最后由TomcatSend a response to the client).

img

How the client accesses the server softwarejava项目

以访问tomcat中的项目为例:

http://IP地址:端口号/项目

http://10.10.128.49:8080/qq/img

使用http协议,通过IPaddress to find the server,Then find the port number again,Finally, select the item to be executed in the port number,In the project, select the folder you want to access,后面还可以加 ? 来添加参数

If the computer that does the server happens to be the current machine(The client is himself,The server is also itself),Then enter your ownIP即可,或者输入:127.0.0.1,或者localhost.完整:http://localhost:8080/) 后面就可以根据webappsThe existing project in the folder to test.

现在在idea中创建的javaItems are normal items,Just have fun for yourself,Others cannot find this in the server through the web pagejava项目,So it should be added for the projectweb模块(项目右键,选择add framework support,选择web),然后就会出现一个web文件夹,In the folder are the resources to be provided to the client.



测试

在web文件夹下创建了一个html文件和一个图片,This is the resource that is ready to be served to the client,But how can the client access it?Server software needs to be installedTomcat,These resources need to be deployed toTomcat中,然后Tomcat利用默认的8080Port to listen for resources and requests sent by clients,然后Tomcatresources are sent out.

So the next step should startTomcat,并把项目部署到Tomcat中.在idea中点击add configuration,在其中找到tomcat server(local的),输入地址后,It will be automatically recognized on the computertomcat,(这时vshas already been deployedtomcat,但是tomcatIt is not clear what resources to manage),继续点development,Select the current project to deploy toTomcat中.TomcatMany projects will be deployed in the future,是通过Application contextto distinguish the search,So this logo can be changed to be more refined,project1->p1,然后就可以通过/p1to find the corresponding resource service.

http://IP地址:端口号/资源路径

http://127.0.0.1:8080/p1

使用http协议,通过IP地址找到服务器,Found by port numberTomcat,Find resources by resource path,It has now been foundproject1项目中的web文件夹,然后再添加/scene.jpgYou can access the pictures.(First make sure that in the upper right cornertomcat已经打开)

When attributes such as resource folders are changed,可以按debug,选择rodeplay重新部署.





登录页面

login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>

<form action="/p1">
    <div>用户名<input name="username"></div>
    <div>密码<input name="password"></div>
    <button type="submit">登录</button>>
</form>

</body>
</html>
http://127.0.0.1:8080/p1/login.html

The address of the server submitted here is setp1项目,You can also write the full name,也可以直接/p1(自动的将/p1拼接到8080后面,Because the current location is8080The web files in the project under the port).Upload the entered username and password to p1中来,但是p1There will be no special response,It just jumps to by defaultp1的index.jsp



Responds to data uploaded on the login page

需要使用servlet技术,需要引用tomcat的库.

因为idea中已经引用了tomcat,所以直接在idea中引用即可.file->project Structure->modules->dependencies->±>library->tomcat库->ok.这样在external library中就可以找到tomcat库.

创建一个LoginServlets类

1,继承HttpServlet,才能处理HTTP请求

2,使用@WebServlet,说明处理的请求路径

http://127.0.0.1:8080/p1/login



LoginServlet

package com.kevin.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet{
    
    //处理get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
        System.out.println("doGet----");
    }

    //处理post请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
        System.out.println("doPost----");
    }
}

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>

<form action="/p1/login" method="get">
    <div>用户名<input name="username"></div>
    <div>密码<input name="password"></div>
    <button type="submit">登录</button>>
</form>

</body>
</html>

The uploaded server address is under the projectlogin,The upload form isget

This is after input,will print in the backgrounddoGet----,parameters and displayed directly on the URL.





receive and respond

HttpServletRequest req : 请求:Used to get the data sent by the client

HttpServletResponse resp:响应:Used to return data to the client

LoginServlet

package com.kevin.servlet;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet{
    
    //处理get请求
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
       doPost(request,response);
    }

    //处理post请求
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if("123".equals(username)&&"456".equals(password))
        {
    
            response.getWriter().write("Login Success!!");
        }
        else
        {
    
            response.getWriter().write("Login Failure!!");
        }
    }
}

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>

<form action="/p1/login" method="post">
    <div>用户名<input name="username"></div>
    <div>密码<input name="password"></div>
    <button type="submit">登录</button>>
</form>

</body>
</html>

用户名123 密码:456

页面显示成功

原网站

版权声明
本文为[It's classmate Xiaoming.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110906450456.html