副手
12879 ワード
スプリングガイドにサーボプレートを使用する
フロントはWASを直接取り付けて上に置くことができますが、これは不便です.そのため、SpringBootで簡単にBritを実行できます.
@サーブレットComponentScanは、サービスボードの自動登録機能です.
@WebServlet(name = "helloServlet", urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("HelloServlet.service");
System.out.println("request = " + request);
System.out.println("response = " + response);
}
}
サーブレットに@Webサーブレット宣言が付属しています.名前を指定したり、urlで働いているurlを指定したりすることができます.要求は相手の要求であり、応答は私が送信する応答である.
サーブレットを使用すると、クエリ文要求を受信したり、レスポンスを送信したりできます.
index.html
mainルートディレクトリにwebappディレクトリを作成し、indexを作成します.htmlが生成されると、localhost:8080はIndexに自動的にリンクされます.
警告

これだけでいいのですが、
サーブレット/servletはこのように二重に入ることはできません...
HttpServletRequest
HTTPリクエストメッセージを簡単に検索できる機能です.
保存:request.setAttribute(name, value)
クエリー:request.getAttribute(name)
また、セッション管理機能も備えています.
request.getSession(create: true)
HTTP要求データ—概要
HTTPリクエストメッセージを介してクライアントからサーバにデータを転送する方法は主に以下の3つがある.
GET-クエリーパラメータ
/url?username=hello&age=20
メッセージ本体がない場合、URLのクエリーパラメータにデータを含む転送検索、フィルタリング、ページングなどでよく使われる方式.
クエリー・パラメータはURLに次のように表示されます.最初から送信できます.追加パラメータは&で区切ります.
http://localhost:8080/request-param?username=hello&age=20
パラメータクエリは大体3つあります.1.すべてのパラメータを表示
2.単一パラメータクエリー
3.複数パラメータの問合せ
POST - HTML Form
content-type: application/x-www-form-urlencoded
クエリー・パリ・テーブルの形式でメッセージ・ボディusername=hello&age=20に渡す
主に会員登録、商品の注文、HTMLフォームの使用を含む
使用するFormの動作と方法は次のとおりです.
<form action="/request-param" method="post">
すなわち,post方式で作成したservletに渡す.
このように送信すると、request-paramでpost方式に移行することがわかります.


POSTMAN利用
テストのたびにFormを作成するのは難しいので、Postを使用できます.


HTTPメッセージ本文に直接データを入れて要求する
主にHTTP API、JSON、XML、TEXTに用いられる
データフォーマットは主にJSONを使用
POST、PUT、PATCHをサポートする


このように入れたメッセージbodyは直接出力されます.
しかし最近はjsonを使って通信している.


private final ObjectMapper objectMapper = new ObjectMapper();
jacksonライブラリを使用して、jsonの内部コンテンツをオブジェクトに変換します.HttpServletResponse
HTTPレスポンスメッセージの生成
指定
作成
基本的な使い方
@WebServlet(name = "responseHeaderServlet", urlPatterns = "/response-header")
public class ResponseHeaderServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setStatus(HttpServletResponse.SC_OK);
response.setHeader("Content-Type", "text/plain;charset=utf-8");
response.setHeader("Cache-Control", "no-cache, no-store, mustrevalidate");
response.setHeader("Pragma", "no-cache");
response.setHeader("my-header","hello");

このように入れるとhtmlヘッダが生成されて私に送られてきたことがわかります.
それ以外は.
内容、Cookie、Redirect関連編の方法があります.

HTMLResponse
HTTP応答としてHTMLを返す場合はcontent-typeをtext/htmlとして指定する必要があります
@WebServlet(name = "responseHtmlServlet", urlPatterns = "/response-html")
public class ResponseHtmlServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//Content-Type: text/html;charset=utf-8
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();

JsonResponse
HTTP応答としてJSONに戻る場合はcontent-typeをアプリケーション/jsonとして指定する必要があります.
Jacksonライブラリが提供するObjectMapper.オブジェクトをJSON文字に変更するには、writeValueAsString()を使用します.
JSONは基本的にcharset utf 8で、別に書く必要はありません.

Reference
この問題について(副手), 我々は、より多くの情報をここで見つけました https://velog.io/@blacklandbird/서블릿テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol