java - 我如何返回 jsp 作为此 servlet 的响应?

标签 java html jsp servlets

如何将 jsp 文件设置为 servlet 中的响应?我需要在 jsp 页面中使用什么样的表达式? 这是表格

<html>
<head>
    <title>Select your Hobby</title>
</head>
<body>
<form method="POST" action="SelectHobby">
    <p> Choose a Hobby:
    </p>
    <select name="hobby" size="1">
        <option>horse skiing
        <option>extreme knitting
        <option>alpine scuba
        <option>speed dating
    </select>
    <br><br>
    <center>
        <input type="SUBMIT">
    </center>
</form>
</body>
</html>

servlet

package com.example.web;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HobbyServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html");
        String hobby = request.getParameter("hobby");
    }
}

这是我想在其中看到爱好的jsp

<html>
<head>
    <title>These are your hobbies</title>
</head>
<body>
<%
</body>
</html>

最佳答案

我建议您阅读此文post 。 JSTL 会帮助你。我想你映射了你的servlet。这是一个简单的例子:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
       <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<form method="POST" action="testservlet">
    <p> Choose a Hobby:
    </p>
    <select name="hobby" size="1">
        <option>horse skiing
        <option>extreme knitting
        <option>alpine scuba
        <option>speed dating
    </select>
    <br><br>
    <center>
        <input type="SUBMIT">
    </center>
</form>
</body>
</html>

servlet:

@WebServlet(name = "test", urlPatterns = { "/testservlet" }) 
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;

public Test() {
    super();
}   

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
    String hobby = request.getParameter("hobby");   
    request.setAttribute("theHobby", hobby);
    request.getRequestDispatcher("hobbypage.jsp").forward(request, response);
}

我定义了一个名为 theHobby 的属性来保存第一页中所选爱好的值。我还创建了一个名为 hobbypage.jsp 的页面,我想将值发送到该页面。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
       <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hobby: ${theHobby}
</body>
</html>

使用 JSTL,您可以通过您定义的名称来调用属性,如 ${nameOfAttribute}。

关于java - 我如何返回 jsp 作为此 servlet 的响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59520864/

相关文章:

java - 为什么在方法参数上同步是 "dangerous"

java - 从 Java 和 CLI 向 crontab 添加 cron 表达式时出现问题

android - 在不将 target-densitydpi 设置为 device-dpi 的情况下防止 Android 浏览器旋转缩放

java - 通过 <a href> 和 jSTL 标记将参数从 JSTL 传递到 servlet

java - 如何去掉小于一的 double 中小数点前的前导零?

java - Objective C 中的字符串加密

html - 调整内容与对齐内容之间的区别

javascript - 自动生成html用于上传图片

Java servlet 和身份验证

Spring Boot Whitelabel 错误页面几乎无处不在