サーブレットチュートリアル:JSP入門–サーブレットの例
公開: 2013-07-16これは、ステップバイステップの説明を含む簡単なJSP –サーブレットの例です。 JSPでリクエストパラメータを取得する方法を示します–サーブレットの例。
実行する手順は次のとおりです。
- EclipseIDEを開きます。
- 新しい動的WebプロジェクトCrunchifyJSPServletExampleを作成します。
- HelloCrunchify.javaファイルを作成してHttpServletを拡張します(200のJavaサンプルすべてのリスト)。
- Crunchify.jspファイルを作成します。
- web.xmlファイル(デプロイメント記述子ファイル)。
- web.xmlファイルが表示されない場合は、ここで解決策を見つけてください。
- TomcatWebサーバーでプロジェクトを追加して実行します。
完全なプロジェクト構造は次のとおりです。
ステップ1
HelloCrunchify.java
ファイルを作成します
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package com . crunchify . jsp . servlet ; import java . io . IOException ; import javax . servlet . ServletException ; import javax . servlet . http . HttpServlet ; import javax . servlet . http . HttpServletRequest ; import javax . servlet . http . HttpServletResponse ; import java . io . PrintWriter ; /** * @author Crunchify.com */ public class HelloCrunchify extends HttpServlet { protected void doGet ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { // reading the user input String username = request . getParameter ( "username" ) ; String password = request . getParameter ( "password" ) ; PrintWriter out = response . getWriter ( ) ; out . println ( "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" +" + "http://www.w3.org/TR/html4/loose.dtd\">\n" + "<html> \n" + "<head> \n" + "<meta http-equiv=\"Content-Type\" content=\"text/html; " + "charset=ISO-8859-1\"> \n" + "<title> Crunchify.com JSP Servlet Example </title> \n" + "</head> \n" + "<body> <div align='center'> \n" + "<style= \"font-size=\"12px\" color='black'\"" + "\">" + "Username: " + username + " <br> " + "Password: " + password + "</font></body> \n" + "</html>" ) ; } } |
別の人は読む必要があります:
- Tomcatの起動時にJavaプログラムを自動的に実行する方法
- Spring Web MVC(.jsp)でAJAX、jQueryを使用する方法–例
ステップ2
Crunchify.jsp
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<% @ page language = "java" contentType = "text/html; charset=ISO-8859-1" pageEncoding = "ISO-8859-1" %> < ! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > < html > <style type ="text/css"> body { background-image : url ( 'https://cdn.crunchify.com/wp-content/uploads/2013/03/Crunchify.bg_.300.png' ) ; } </style> < head > < meta http - equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" > < title > Crunchify JSP Servlet Example < / title > < / head > < body > < div align = "center" style = "margin-top: 50px;" > < form action = "CrunchifyServlet" > Please enter your Username : < input type = "text" name = "username" size = "20px" > < br > Please enter your Password : < input type = "text" name = "password" size = "20px" > < br > < br > < input type = "submit" value = "submit" > < / form > < / div > < / body > < / html > |

ステップ-3
web.xml
ファイルを更新します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<? xml version = "1.0" encoding = "UTF-8" ?> < web - app xmlns : xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns : web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi : schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version = "3.0" > < display - name > CrunchifyJSPServletExample < / display - name > < welcome - file - list > < welcome - file > index . html < / welcome - file > < welcome - file > index . htm < / welcome - file > < welcome - file > index . jsp < / welcome - file > < welcome - file > default . html < / welcome - file > < welcome - file > default . htm < / welcome - file > < welcome - file > default . jsp < / welcome - file > < / welcome - file - list > < servlet > < servlet - name > Hello < / servlet - name > < servlet - class > com . crunchify . jsp . servlet . HelloCrunchify < / servlet - class > < / servlet > < servlet - mapping > < servlet - name > Hello < / servlet - name > < url - pattern > / CrunchifyServlet < / url - pattern > < / servlet - mapping > < / web - app > |
ステップ-4
TomcatにCrunchifyJSPServletExample
をデプロイして実行します。
ステップ-5
URLをhttp://localhost:8080/CrunchifyJSPServletExample/Crunchify.jsp
にポイントします
ステップ-6
チェックアウト結果。 URLは次のようになりますhttp://localhost:8080/CrunchifyJSPServletExample/CrunchifyServlet?username=crunchify&password=Password
サーバーを再起動または起動した場合を除いて、サーバーは「 web.xml
」の変更を認識しないことに注意してください。