问题描述
<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>
然后这个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>
.
- 我们的Servlets Wiki页面-包含一个涵盖基本验证的Hello世界.
- Our Servlets wiki page - Contains a Hello world which covers basic validation.
这篇关于Servlet将响应发送到JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!