将数据从servlet传递给jsp而没有表单? [英] Pass data from servlet to jsp without forms?

查看:122
本文介绍了将数据从servlet传递给jsp而没有表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在这里要做的就是从servlet中获取JSP中没有任何形式的酒店列表。

So what I'm basically trying to do here is get a list of hotels in JSP , from the servlet , without any forms.

这是我的JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<ul>
    <c:forEach var="elem" items="${list}">
    <li>${elem.name}</li>
    </c:forEach>    
</ul>
</body>
</html>

Servlet功能:

Servlet function:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws                           ServletException, IOException {


    try {
        java.util.List<Hotel> list = model.getAllHotels();
        request.setAttribute("list", list);
        RequestDispatcher rDispatcher = request.getRequestDispatcher("/index.jsp");
        rDispatcher.forward(request, response);

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

现在我知道如何通过表单获取/ post,因为servlet具有特定的功能。但是如何在没有表单的情况下发送这些数据?

Now I know how to do this via form with get / post , since the servlet has specific functions for that. But how can I send this data without forms ?

推荐答案

您只需要一个指向您的servlet的链接:

You simply need a link to your servlet:

<a href="<c:url value='/yourServlet' />">Click here to list the hotels</a>

您也可以通过在浏览器的地址栏中输入地址来调用servlet:

You can also invoke the servlet by typing its address in the address bar of your browser:

http://localhost:8080/yourWebApp/yourServlet

你的servlet的代码很好,以及JSP的代码。

The code of your servlet is fine, and the code of the JSP as well.

servlet被映射到某个URL (在我的示例中为 / yourServlet ),这要归功于web.xml中的< servlet-mapping> 元素,或者感谢servlet类上的 @WebServlet 注释。

The servlet is mapped to some URL (/yourServlet in my example) thanks to a <servlet-mapping> element in the web.xml, or thanks to a @WebServlet annotation on the servlet class.

这篇关于将数据从servlet传递给jsp而没有表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆