<html>
<head>
</head>
<body>
<form name="loginform" method="post" action="WelcomeServlet">
<br><br>
<table align="center"><tr><td><h2>Login Authentication</h2></td></tr></table>
<table width="300px" align="center" style="border:1px solid #000000;background-color:#efefef;">
<tr><td colspan=2></td></tr>
<tr><td colspan=2> </td></tr>
<tr>
<td><b>Login Name</b></td>
<td><input type="text" name="username" ></td>
</tr>
<tr>
<td><b>Password</b></td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
<tr><td colspan=2> </td></tr>
</table>
</form>
</body>
</html>
このサーブレット
import java.io.*;
import java.util.*;
//import java.io.PrintWriter;
import javax.servlet.*;
//import javax.servlet.ServletConfig;
//import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WelcomeServlet extends HttpServlet {
/*
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Get the value of form parameter
*/
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//out.println("I am on welcome servlet...");
String username = request.getParameter("username");
String password =request.getParameter("password");
out.println("<html>");
out.println("<head>");
out.println("<title> A very simple servlet example</title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
if((username.equals("kiran"))&&(password.equals("kiran")))
{
String welcomeMessage = "Welcome "+username+" thanks for login...";
out.println("<h1>"+welcomeMessage+"</h1>");
request.getRequestDispatcher("/login.jsp").include(request, response);
}else
{
out.println("<h1> You are not the valid user...</h1>");
request.getRequestDispatcher("/login.jsp").include(request, response);
}
out.println("</html>");
out.close();
}
public void destroy() {
}
}
JSPページのログイン認証テーブルの下に表示されるServtからの応答が欲しいのですが。
ベストアンサー
それは完全には正しくありません。基本的なサーブレットチュートリアルの多くがあなたに信じさせようとしているのとは対照的に、servletは純粋なHTMLを出力するために使用されるべきではありません。これはMVCのイデオロギーと矛盾します。そこにJSPが使用されるべきです。
この場合は、JSPに表示するメッセージをサーブレットにリクエストスコープに設定してもらい、そのリクエスト/レスポンスをJSPに転送する必要があります。 JSPでは、JSTLを使用してHTML出力を動的に制御し、EL $ {}を使用してメッセージにアクセスして表示することができます。
これがサーブレットの外観の例です。
public class WelcomeServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String message = null;
if ((username.equals("kiran")) && (password.equals("kiran"))) {
message = "Welcome "+username+" thanks for login...";
} else {
message = "You are not the valid user...";
}
request.setAttribute("message", message);
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
そしてlogin.jspを編集して以下を追加します。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:if test="${not empty message}">
<h1>${message}</h1>
</c:if>
taglib宣言は一番上に行かなければなりません。 < c:if> < h1>を表示したい場所に正確に配置できます。
また見なさい:
> Our Servlets wiki page – 基本的なバリデーションをカバーするHello Worldが含まれています。
関連記事
- java.lang.IllegalStateException:サーブレットで応答がコミットされた後で転送できない
- java - 要求ごとに2つのサーブレット応答を送信する
- Javaサーブレット内からチャンク応答でHttpトレーラー/フッターを送信する方法
- HTMLフォームデータ配列をJSP /サーブレットに送信する
- HTMLフォームを送信するときにJSPからサーブレットにデータを転送する方法
- java - サーブレットからパラメータを送信する方法
- asp.net-mvc - ASP.NET MVC Web APIのCache-Controlヘッダーが応答オブジェクトに設定されているにもかかわらず応答で送信されない
- node.js - Nodejsが応答してファイルを送信する
転載記事の出典を記入してください: サーブレットがJSPに応答を送信する - コードログ