-2
サーブレットで定義された変数をJSPに出力しようとしていますが、できません。サーブレットからJSPに変数を出力する方法は?
ここに私のコードです:
MainServlet:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package DAO;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "MainServlet", urlPatterns = {"/MainServlet"})
public class MainServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
request.setAttribute("msg", "Bloody Hell"); // Here <<<<<<<<<<<<<<<
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
のindex.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>BlueBank</title>
</head>
<body>
<h1>Whatever: ${msg}</h1>
</body>
</html>
なぜ表示されるのですか?表示したJSPでサーブレットとどのような関係がありますか? –
Spring MVCのように扱いやすいものではなく、生のサーブレットを使う必要がありますか? – chrylis