使用以下servlet代码
package com.example.tutorial;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletExample extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("firstname") == null || request.getParameter("lastname") == null){
this.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
return;
}
this.getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
}
}
我首先使用index.jsp
接收请求参数,如下所示:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<form action="servletexample" method="post" >
<table border="0">
<tr>
<td>First Name:</td> <td><input type="text" name="firstname" /></td>
</tr>
<tr>
<td>Last Name:</td> <td><input type="text" name="lastname" /></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
然后servlet使用output.jsp
与requestdispatcher
进行通信,如下所示:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h1>Your first and last name is: </h1>
<%
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
out.print(firstName + " " + lastName);
%>
</body>
</html>
在上面的output.jsp
代码中,request.getParameter
方法用于从servlet中检索转发的参数。
现在,我只想使用html文件[使用javascript(如果需要)],而不是output.jsp
,
html body的内容应该是什么?
注意:更进一步,意图是使用html / javascrip / css作为前端和基于Java的Web框架(如Spring)作为后端。我目前对servlets / jsp的学习是顺利过渡到Spring框架(后端),可以使用html / javascrip / css
答案 0 :(得分:1)
来自你的评论:
我想要,Javascript操作与服务器通信并更改显示。基本上我想看html / JavaSript / css作为frontend和servlet(仅)作为后端
然后最好使用ajax请求并使用JSON来编写响应而不是转发。然后,您的前端可能是纯HTML或其他技术,如移动应用程序。
请注意,如果您打算使用Servlet的唯一目的是创建RESTful服务,那么最好使用REST方法而不是自己编写servlet。例如,你有两个选择:
如果您对Java不熟悉,我建议您使用JAX-RS。如果你已经有过Java和Spring的经验,那么我建议你使用Spring,使用Spring Boot它是一个选项,它取决于你的特定情况。
答案 1 :(得分:0)
首先java是基于编译器的语言,html是基于解释器的语言。所以你不能在html文件中编写java代码。可以用jsp编写。 如果你想使用html编写java代码,你可以。
创建html文件output.html 使用此代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Your first and last name is: </h1>
</body>
</html>
之后,只需用此
替换RequestDispatcher转发语句的语句this.getServletContext().getRequestDispatcher("/output.html").forward(request, response);