Servlet – 表单
网页是标签、文本框、复选框、选项、图像等许多输入元素的组合,可以通过使用Java servlet 将所有输入元素包含在服务器端的“HTML FORM”中来准备.通常,HTML 表单通过这些输入元素从用户那里收集数据,并让我们查看如何使用 HTTP GET/POST 方法将它们发送到服务器端。
流动:
- 请求将从 HTML 表单发送
- 它将作为Java servlet 端的GET/POST方法发送
- 响应将从 servlet 作为 HTML 表单提供
需要知道以下标签来处理流程 Tag Name UsageTo create a form in an HTML page and it encloses all the input elements. To create a text box or password or submit etc., To create dropdown To create a textarea
除了表单标签,我们还必须将方法指定为“GET”或“POST”。通常敏感数据必须作为“POST”传递,因为在 URL 中,参数不作为查询参数发送。因此,对于发送“密码”等敏感信息,“发布”方法是一种不错的方法。而且
- 在“Post”方法中,我们可以传递许多参数。
- 发布方法未添加书签
- 不可能保留在浏览器历史记录中,也不能被缓存,因此更安全
“POST”方法用于创建或更新资源
POST / HTTP/1.1
HOST :
param1=value1¶m2=value2
如果我们只传递有限的参数,“Get”方法最适合,而且由于参数是在外面看到的,它不太适合发送敏感参数
GET /?param1=value1¶m2=value2
- “获取”方法可以被缓存、添加书签并保留在历史记录中
- 不适合发送敏感信息,也有长度限制
- 它通常用于检索某一组数据。
此外,除了 form 标签,我们还需要设置 action 标签。它将包含“servlet 的 URL”
1. name
method=”GET/POST”: Used to send the form data as an HTTP POST/GET request to the server.
Generally, form submission containing sensitive data should be done in HTTP POST method.
2. action=”URL of the servlet”: Used to specify relative URL of the servlet
and it is responsible for collecting form data and process accordingly.
表单数据的示例片段;
HTML
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Registration Application
Java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// We can able to get the form data by means of the below ways.
// Form arguments should be matched and then only they are recognised
String userName = request.getParameter("userName");
String yourEmailID = request.getParameter("yourEmailID");
String yourPassword = request.getParameter("yourPassword");
String gender = request.getParameter("gender");
String favoriteLanguage = request.getParameter("favoriteLanguage");
System.out.println("gender.." + gender);
System.out.println("favoriteLanguage.." + favoriteLanguage);
// Here the business validations goes. As a sample,
// we can check against a hardcoded value or pass the
// values into a database which can be available in local/remote db
// For easier way, let us check against a hardcoded value
if (userName != null && yourEmailID != null && yourPassword != null ) {
// We can redirect the page to a welcome page
// Need to pass the values in session in order
// to carry forward that one to next pages
HttpSession httpSession = request.getSession();
httpSession.setAttribute("emailId", yourEmailID);
httpSession.setAttribute("gender", gender);
httpSession.setAttribute("favoriteLanguage", favoriteLanguage);
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}
}
}
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String gender = (String)session.getAttribute("gender");
String genderTitle = null;
if (gender != null && gender.equalsIgnoreCase("female")) {
genderTitle = "Ms/Mrs.";
} else {
genderTitle = "Mr.";
}
%>
Welcome <%= genderTitle %> <%=session.getAttribute("emailId") %>
Your Resume has been added under <%= session.getAttribute("favoriteLanguage") %> POOL
我们可以通过 CSS(Cascading StyleSheet)美化表单,也可以通过 Javascript 验证表单。客户端验证在 javascript 中处理。让我们为示例登录表单执行此操作。由于我们需要处理验证,我们可以通过 JSP 文件获取输入
注册.jsp
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
Registration Application
在上面的代码中
Please select your favorite language:
输出:
前端电子邮件验证是通过 register.jsp 文件中的“ValidateEmail”方法完成的。我们可以将 CSS 在单独的填充和 javascript 中拆分为单独的文件并包含它们。
如果电子邮件 ID 格式不正确,我们将收到错误消息。前端验证结束后,表单将提交给 action 标签中提到的相应 servlet。
注册Servlet。Java
Java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// We can able to get the form data by means of the below ways.
// Form arguments should be matched and then only they are recognised
String userName = request.getParameter("userName");
String yourEmailID = request.getParameter("yourEmailID");
String yourPassword = request.getParameter("yourPassword");
String gender = request.getParameter("gender");
String favoriteLanguage = request.getParameter("favoriteLanguage");
System.out.println("gender.." + gender);
System.out.println("favoriteLanguage.." + favoriteLanguage);
// Here the business validations goes. As a sample,
// we can check against a hardcoded value or pass the
// values into a database which can be available in local/remote db
// For easier way, let us check against a hardcoded value
if (userName != null && yourEmailID != null && yourPassword != null ) {
// We can redirect the page to a welcome page
// Need to pass the values in session in order
// to carry forward that one to next pages
HttpSession httpSession = request.getSession();
httpSession.setAttribute("emailId", yourEmailID);
httpSession.setAttribute("gender", gender);
httpSession.setAttribute("favoriteLanguage", favoriteLanguage);
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}
}
}
欢迎.jsp
HTML
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String gender = (String)session.getAttribute("gender");
String genderTitle = null;
if (gender != null && gender.equalsIgnoreCase("female")) {
genderTitle = "Ms/Mrs.";
} else {
genderTitle = "Mr.";
}
%>
Welcome <%= genderTitle %> <%=session.getAttribute("emailId") %>
Your Resume has been added under <%= session.getAttribute("favoriteLanguage") %> POOL
输出:
通过视频解释表单概念:
该视频还解释了“Onsubmit”和“OnClick”事件之间的区别。
结论
表单被认为是从用户那里收集所需信息的输入场所。它可以存在于任何前端文件中,如 HTML/JSP 等,如果将它放在 JSP(Java服务器页面)中,我们可以轻松处理 javascript 验证。 Javascript 验证需要在客户端本身进行处理。因此 JSP 类型的页面很好。根据表单标签请求,将执行“POST”或“GET”方法,并再次将响应写回 JSP 页面。为了将表单数据从一个页面传送到另一个“会话”。