본문 바로가기

카테고리 없음

Sample Web Server Program Java

This tutorial will show you how to build a simple web app using Spring Framework and STS (Spring Tool Suite). It’s a beginner guide and it will help you if you are new to Spring or STS.NOTE: This tutorial requires Spring STS to be installed and configured with Eclipse. If you haven’t done this step yet please read first. What is Spring BootThis tutorial uses Spring Boot to create a simple web-app with running on embedded Tomcat server.Spring Boot is Spring’s solution for creating stand-alone applications that are easy to build and run. It uses preconfigured Spring platform and third-party libraries so you can get started within minutes. Most Spring Boot applications need very little Spring configuration. This particular example does not need any configuration at all.Features of Spring Boot:.

Create stand-alone Spring applications. Embed Tomcat or Jetty directly (no need to deploy WAR files). Provide POM file to simplify your Maven configuration. Automatically configure Spring whenever possible. Provide production-ready features such as metrics, health checks and externalized configuration. Absolutely no code generation and no requirement for XML configurationCreate New Spring Starter Project1. Start Eclipse and go to File - New - Other or press Ctrl+N on your keyboardIn the search field type “spring”.

This will list several Spring options. The one we need for this tutorial is “Spring Starter Project”. Select it and confirm with “Next” button.

DiscussionAs you can see, this sample code is a complete Java socket client class that you can use for your own tests. On my computer this code currently opens a socket to my local computer (“localhost”) on port 8080 (my local server), and if that succeeds, it sends to the command GET /nn to Tomcat.

Sample Web Server Program Java 10

Sample

Sample Web Server Program Java Server

This is an HTTP command that tells Tomcat to return its root-level document, essentially the same as typing in your browser.The program writes that to Tomcat, gets the results back from Tomcat, and writes the results to standard out (using System.out.println).As documented in the code, you might want to add a convenience method to this class (or change the current method) so you can set time timeout value dynamically. What I mean by that is that you might want to add a new method with this signature:Socket openSocket(String server, int port, int timeout) throws ExceptionAs a final note, here’s a link to more information on, i.e., the number of milliseconds before your socket is considered to be “timed out” in communicating with a server.