Servlet 教程:JSP 入门 - Servlet 示例
已发表: 2013-07-16这是一个简单的 JSP – Servlet 示例,其中包含分步说明。 我将演示如何在 JSP – Servlet 示例中检索请求参数。
以下是我们将要执行的步骤:
- 打开 Eclipse IDE。
- 创建新的动态 Web 项目CrunchifyJSPServletExample。
- 创建HelloCrunchify.java文件扩展 HttpServlet(所有 200 个 Java 示例的列表)。
- 创建Crunchify.jsp文件。
- web.xml文件(部署描述符文件)。
- 如果您没有看到 web.xml 文件,请在此处找到解决方案。
- 在 Tomcat Web 服务器上添加并运行项目。
这是一个完整的项目结构:
第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 > |

第三步
更新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 > |
第四步
在 Tomcat 上部署并运行CrunchifyJSPServletExample
。
第 5 步
将您的 URL 指向http://localhost:8080/CrunchifyJSPServletExample/Crunchify.jsp
第 6 步
结帐结果。 您的 URL 应如下所示: http://localhost:8080/CrunchifyJSPServletExample/CrunchifyServlet?username=crunchify&password=Password
请注意,除非您重新启动或启动了服务器,否则服务器无法识别“ web.xml
”中的更改。