Servlet将响应发送到JSP [英] Servlet send response to JSP

查看:63
本文介绍了Servlet将响应发送到JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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>&nbsp;</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>&nbsp;</td></tr>
</table>
</form>


</body>
</html>

然后这个servlet

then this servlet

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页面登录身份验证表下方的servet的响应

I want a response from servet which is display below the jsp page login authetication table

推荐答案

那并不完全正确.与许多基本的servlet教程试图让您相信的相反, servlet 应该用于输出纯HTML.这与MVC的思想相矛盾.此处应使用 JSP .

That's not entirely right. In contrary to what lot of basic servlet tutorials try to let you believe, the servlet should not be used to output pure HTML. This contradicts the MVC ideology. There the JSP should be used for.

在这种情况下,您需要让servlet在请求范围内设置要显示在JSP中的消息,然后将请求/响应转发到JSP.在JSP中,您可以使用 JSTL 动态控制HTML输出,并使用

In this particular case, you need to let the servlet set the message which you'd like to display in the JSP in the request scope and then forward the request/response to the JSP. In the JSP, you can use JSTL to dynamically control the HTML output and use EL ${} to access and display the message.

这是servlet外观的启动示例:

Here's a kickoff example of how the servlet should look like:

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以添加以下内容:

and edit your login.jsp to add the following:

<%@ 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>的位置.

The taglib declaration has to go in the top. The <c:if> can just be positioned exactly there where you'd like to display the <h1>.

  • Our Servlets wiki page - Contains a Hello world which covers basic validation.

这篇关于Servlet将响应发送到JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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