使用注解隐藏表单域 | Java小服务程序
隐藏表单域用于存储客户端的会话信息。在这个方法中,我们创建了一个隐藏表单,它将控件传递给 servlet,其路径在表单操作区域中给出。使用它,用户的信息被存储并传递到我们想要发送数据的位置。
使用隐藏表单的主要优点是它不依赖于浏览器。即使 cookie 被禁用或未隐藏,提交的表单也能正常工作。
例子
Java
out.print("");
HTML
Insert title here
Java
// Java program to demonstrate
// Hidden form field method
package GeeksforGeeks;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/FirstServlet")
// this annotation is used for replacing xml file
public class FirstServlet extends HttpServlet {
// class name is FirstServlet which extends HttpServlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
try {
response.setContentType("text/html");
/*
The response's character encoding is only set from the given
content type if this method is called before getWriter is called.
This method may be called repeatedly to change content type and
character encoding.
*/
PrintWriter out = response.getWriter();
/*
The Java PrintWriter class ( java.io.PrintWriter ) enables you to
write formatted data to an underlying Writer . For instance,
writing int, long and other primitive data formatted as text,
rather than as their byte values
*/
String username = request.getParameter("userName");
/*
request.getParameter takes the value from index.html file where
name is username
*/
out.print("Welcome " + username);
// out.println is used to print on the client web browser
/*
In the below code their is a hidden form
for maintaining session of user.
this passes control to SecondServlet
*/
out.print("");
out.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
Java
// Java program to demonstrate
// Hidden form field method
package GeeksforGeeks;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet; // Importing annotation
import javax.servlet.http.*;
// using this annotation we dont need
// xml file for dispathing servlet
@WebServlet("/SecondServlet")
public class SecondServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try {
response.setContentType("text/html");
/*
The response's character encoding is only set from the given
content type if this method is called before getWriter is called.
This method may be called repeatedly to change content type and
character encoding.
*/
PrintWriter out = response.getWriter();
/*
The Java PrintWriter class ( java.io.PrintWriter ) enables you to
write formatted data to an underlying Writer . For instance,
writing int, long and other primitive data formatted as text,
rather than as their byte values
*/
// getting value from the query string
String username = request.getParameter("username");
// taking the value of usename from First servlet using getparameter object
out.print("WELCOME " + username);
// out.println is used to print on the client web browser
out.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
隐藏表单域方法的完整示例
在这种方法中,我们将客户端详细信息从 firstServlet 传输到 Second Servlet
索引.html
HTML
Insert title here
输出
第一个Servlet。Java
Java
// Java program to demonstrate
// Hidden form field method
package GeeksforGeeks;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/FirstServlet")
// this annotation is used for replacing xml file
public class FirstServlet extends HttpServlet {
// class name is FirstServlet which extends HttpServlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
{
try {
response.setContentType("text/html");
/*
The response's character encoding is only set from the given
content type if this method is called before getWriter is called.
This method may be called repeatedly to change content type and
character encoding.
*/
PrintWriter out = response.getWriter();
/*
The Java PrintWriter class ( java.io.PrintWriter ) enables you to
write formatted data to an underlying Writer . For instance,
writing int, long and other primitive data formatted as text,
rather than as their byte values
*/
String username = request.getParameter("userName");
/*
request.getParameter takes the value from index.html file where
name is username
*/
out.print("Welcome " + username);
// out.println is used to print on the client web browser
/*
In the below code their is a hidden form
for maintaining session of user.
this passes control to SecondServlet
*/
out.print("");
out.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出
第二个小服务程序
Java
// Java program to demonstrate
// Hidden form field method
package GeeksforGeeks;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet; // Importing annotation
import javax.servlet.http.*;
// using this annotation we dont need
// xml file for dispathing servlet
@WebServlet("/SecondServlet")
public class SecondServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try {
response.setContentType("text/html");
/*
The response's character encoding is only set from the given
content type if this method is called before getWriter is called.
This method may be called repeatedly to change content type and
character encoding.
*/
PrintWriter out = response.getWriter();
/*
The Java PrintWriter class ( java.io.PrintWriter ) enables you to
write formatted data to an underlying Writer . For instance,
writing int, long and other primitive data formatted as text,
rather than as their byte values
*/
// getting value from the query string
String username = request.getParameter("username");
// taking the value of usename from First servlet using getparameter object
out.print("WELCOME " + username);
// out.println is used to print on the client web browser
out.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}
输出
逐步处理代码
当您部署项目并在服务器上运行代码时,以下过程会继续进行。
- 控制转到 index.html 文件,并在 Web 浏览器上生成一个表单,它会询问您的用户名。
- 在 index.html 文件表单中,操作是 FirstServlet,因此当您点击提交按钮时,控制权转到 FirstServlet 并开始您的会话。
- 在 FirstServlet request.getParameter(username) 中,从 index.html 中获取输入,其中字段是用户名并在浏览器上打印。
- 在 firstServlet 中有一个隐藏的表单字段,它再次从浏览器获取输入并将控制权传递给第二个 servlet。
- 这种方法适用于许多不同的 Servlet。
注意:这是隐藏表单域方法的主要缺点,您必须一次又一次地填写表单以维护客户端会话。 - 在 Second Servlet 中,我们再次使用 request.getParameter 从 FirstServlet 页面获取输入。
优势与应用
- 它可用于匿名会话跟踪。
- 每个浏览器都支持隐藏表单域。这种会话跟踪方法不需要对浏览器进行任何特殊配置。
- 所有信息都存储在客户端浏览器中,因此增加了安全性
坏处 :
- 它仅适用于一系列动态生成的表单。这与静态文档、电子邮件文档、书签文档和浏览器关闭有关。
- 您需要针对每个请求提交额外的表格。
- 它比 URL 重写复杂。
- 此方法仅使用文本信息。