Servlet的使用

创建一个Servlet项目并实现从前端JSP或分离的HTML请求Servlet的基本步骤如下:

步骤一:创建Servlet项目

  1. 使用IDE(如Eclipse、IntelliJ IDEA或NetBeans)创建一个新的Java Web项目,选择Java Web项目模板,并确保项目中包含Servlet支持(如使用JavaEE或JakartaEE标准)。

2、JSP或者Html提交请求参数

2.1 JSP版本

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Servlet Parameter Example</title>
</head>
<body>
    <form action="MyServlet" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username">
        <br>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password">
        <br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

2.2 HTML版本:

如果HTML页面独立于Web应用,需要确保Servlet应用部署后可通过URL访问,并在HTML中设置表单action属性指向Servlet的URL:

<!DOCTYPE html>
<html>
<head>
    <title>Servlet Request Example</title>
</head>
<body>

<form action="http://127.0.0.1:8080/your-app-context-path/MyServlet" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username">
        <br>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password">
        <br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

步骤二:创建Servlet类

  1. 在项目的src目录下创建一个新类,继承自javax.servlet.http.HttpServlet。例如:
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("/MyServlet")
public class MyServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        // 从请求中获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 检查参数是否为空
        if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
            // 参数处理...
            System.out.println("Received username: " + username);
            System.out.println("Received password: " + password);

            // 示例回应
            response.setContentType("text/plain");
            response.getWriter().println("Your username and password have been received.");
        } else {
            // 参数为空的情况处理
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Username and password must not be empty.");
        }
    }
}

在上述代码中,@WebServlet("/MyServlet")注解表明该Servlet的URL映射为/MyServlet

步骤三:配置web.xml(可选)

对于较新版的Servlet容器(如Tomcat 8及以上版本),注解方式足以配置Servlet。但如果使用旧版本或者想在web.xml中配置,需要添加以下内容:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>com.example.MyServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/MyServlet</url-pattern>
    </servlet-mapping>
    
    <!-- 其他配置... -->
    
</web-app>

步骤四:部署与测试

  1. 将项目部署到Servlet容器(如Apache Tomcat)中。
  2. 启动服务器并访问前端页面(如http://localhost:8080/your-app-context-path/index.jsp)。
  3. 点击表单按钮触发请求,Servlet将处理请求并生成响应内容。

现在当你访问前端页面并提交表单或直接访问/MyServlet时,Servlet会响应并显示你在Servlet中生成的HTML内容。

原文链接 https://www.hanyuanhun.cn | https://node.hanyuanhun.cn

posted @   汉源魂  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示