<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
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() {
}
}
我想要来自servet的响应,它显示在jsp页面登录验证表
下面答案 0 :(得分:35)
这不完全正确。与许多基本servlet教程试图让你相信的相反,servlet应不用于输出纯HTML。这与MVC意识形态相矛盾。应该使用JSP。
在这种特殊情况下,您需要让servlet设置您希望在请求范围内的JSP中显示的消息,然后转发对JSP的请求/响应。在JSP中,您可以使用JSTL动态控制HTML输出,并使用EL ${}
来访问和显示消息。
这是一个关于servlet应该如何的启动示例:
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>
的位置。