当前位置: 首页 > news >正文

衡水企业做网站费用/百度站长平台账号购买

衡水企业做网站费用,百度站长平台账号购买,素材网网站建设,自己建网站需要怎么做以下内容引用自http://wiki.jikexueyuan.com/project/servlet/form-data.html: 当需要从浏览器到Web服务器传递一些信息并最终传回到后台程序时,一定遇到了许多情况。浏览器使用两种方法向Web服务器传递信息。这些方法是GET方法和POST方法。 一、GET方法…

以下内容引用自http://wiki.jikexueyuan.com/project/servlet/form-data.html:

当需要从浏览器到Web服务器传递一些信息并最终传回到后台程序时,一定遇到了许多情况。浏览器使用两种方法向Web服务器传递信息。这些方法是GET方法和POST方法。

一、GET方法

GET方法向页面请求发送已编码的用户信息。页面和已编码的信息用?字符分隔,如下所示:

http://www.test.com/hello?key1=value1&key2=value2

GET方法是从浏览器向Web服务器传递信息的默认的方法,且它会在浏览器的地址栏中产生一个很长的字符串。如果向服务器传递密码或其他敏感信息,请不要使用GET方法。GET方法有大小限制:请求字符串中最多只能有1024个字符。

这些信息使用QUERY_STRING头传递,并通过QUERY_STRING环境变量访问,Servlet使用doGet()方法处理这种类型的请求。

二、POST方法

一般情况下,将信息传递给后台程序的一种更可靠的方法是POST方法。POST方法打包信息的方式与GET方法相同,但是POST方法不是把信息作为URL中?字符之后的文本字符串进行发送,而是把它作为一个单独的消息发送。消息以标准输出的形式传到后台程序,可以在处理过程中解析并使用这些标准输出。Servlet使用doPost()方法处理这种类型的请求。

三、使用Servlet读取表单数据

Servlet以自动解析的方式处理表单数据,根据不同的情况使用如下不同的方法:

  • getParameter():可以调用request.getParameter()方法来获取表单参数的值。

  • getParameterValues():如果参数出现不止一次,那么调用该方法并返回多个值,例如复选框。

  • getParameterNames():如果想要得到一个当前请求的所有参数的完整列表,那么调用该方法。

四、使用URL的GET方法实例

这是一个简单的URL,使用GET方法将两个值传递给HelloForm程序。

http://localhost:8080/HelloForm?first_name=ZARA&last_name=ALI

下面是HelloForm.java Servlet程序,处理由Web浏览器给定的输入。将使用getParameter()方法,使访问传递的信息变得非常容易:

// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloForm extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{// Set response content typeresponse.setContentType("text/html");PrintWriter out = response.getWriter();String title = "Using GET Method to Read Form Data";String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " +"transitional//en\">\n";out.println(docType +"<html>\n" +"<head><title>" + title + "</title></head>\n" +"<body bgcolor=\"#f0f0f0\">\n" +"<h1 align=\"center\">" + title + "</h1>\n" +"<ul>\n" +"  <li><b>First Name</b>: "+ request.getParameter("first_name") + "\n" +"  <li><b>Last Name</b>: "+ request.getParameter("last_name") + "\n" +"</ul>\n" +"</body></html>");}
}

假设环境已经正确地设置,编译HelloForm.java,如下所示:

$ javac HelloForm.java

如果一切顺利,上述编译会产生HelloForm.class文件。接下来,需要把这个类文件复制到<Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes中,并在<Tomcat-installation-directory>/webapps/ROOT/WEB-INF/中的web.xml文件中创建以下条目:

    <servlet><servlet-name>HelloForm</servlet-name><servlet-class>HelloForm</servlet-class></servlet><servlet-mapping><servlet-name>HelloForm</servlet-name><url-pattern>/HelloForm</url-pattern></servlet-mapping>

注意:POM和Eclipse项目直入主体修改web.xml。

现在在浏览器的地址栏中输入http://localhost:8080/HelloForm?first_name=ZARA&last_name=ALI,并在浏览器中触发上述命令之前,确保已经启动Tomcat服务器。这将产生如下所示的结果:

五、使用表单的GET方法实例

下面是一个简单的实例,使用HTML表单和提交按钮传递两个值。将使用相同的Servlet HelloForm来处理这个输入。

<html>
<body>
<form action="HelloForm" method="GET">
First Name: <input type="text" name="first_name">
<br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

将这个HTML保存到hello.htm文件中,并把它放在<Tomcat-installation-directory>/webapps/ROOT目录下。当访问http://localhost:8080/Hello.htm时,下面是上述表单的实际输出。

尝试输入姓名,然后点击提交按钮来在tomcat运行的本地计算机上查看结果。基于提供的输入,它会产生与上述例子中相似的结果。

六、使用表单的POST方法实例

对上述Servlet做一点修改,以便它可以处理GET方法和POST方法。下面是HelloForm.java Servlet程序,使用GET和POST方法处理由Web浏览器给出的输入。

// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloForm extends HttpServlet {// Method to handle GET method request.public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{// Set response content typeresponse.setContentType("text/html");PrintWriter out = response.getWriter();String title = "Using GET Method to Read Form Data";String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " +"transitional//en\">\n";out.println(docType +"<html>\n" +"<head><title>" + title + "</title></head>\n" +"<body bgcolor=\"#f0f0f0\">\n" +"<h1 align=\"center\">" + title + "</h1>\n" +"<ul>\n" +"  <li><b>First Name</b>: "+ request.getParameter("first_name") + "\n" +"  <li><b>Last Name</b>: "+ request.getParameter("last_name") + "\n" +"</ul>\n" +"</body></html>");}// Method to handle POST method request.public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}

现在编译,部署上述Servlet,并使用带有POST方法的Hello.htm测试它,如下所示:

<html>
<body>
<form action="HelloForm" method="POST">
First Name: <input type="text" name="first_name">
<br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

这是上述表单的实际输出,尝试输入姓名,然后点击提交按钮,在 tomcat 运行的本地计算机上查看结果。

基于提供的输入,它会产生与上述例子中相似的结果。

七、将复选框数据传递到Servlet程序

当要选择多个选项时,就要使用复选框。

这是一个HTML代码实例,CheckBox.htm,一个表单带有两个复选框。

<html>
<body>
<form action="CheckBox" method="POST" target="_blank">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics"  /> Physics
<input type="checkbox" name="chemistry" checked="checked" /> Chemistry
<input type="submit" value="Select Subject" />
</form>
</body>
</html>

这段代码的结果是如下所示的表单:

下面是CheckBox.java Servlet程序,来为复选框按钮处理Web浏览器给定的输入。

// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class CheckBox extends HttpServlet {// Method to handle GET method request.public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{// Set response content typeresponse.setContentType("text/html");PrintWriter out = response.getWriter();String title = "Reading Checkbox Data";String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " +"transitional//en\">\n";out.println(docType +"<html>\n" +"<head><title>" + title + "</title></head>\n" +"<body bgcolor=\"#f0f0f0\">\n" +"<h1 align=\"center\">" + title + "</h1>\n" +"<ul>\n" +"  <li><b>Maths Flag : </b>: "+ request.getParameter("maths") + "\n" +"  <li><b>Physics Flag: </b>: "+ request.getParameter("physics") + "\n" +"  <li><b>Chemistry Flag: </b>: "+ request.getParameter("chemistry") + "\n" +"</ul>\n" +"</body></html>");}// Method to handle POST method request.public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}

配置web.xml:

    <servlet><servlet-name>CheckBox</servlet-name><servlet-class>com.jsoft.testservletbasics.CheckBox</servlet-class></servlet><servlet-mapping><servlet-name>CheckBox</servlet-name><url-pattern>/CheckBox</url-pattern></servlet-mapping>

上面的实例将显示如下所示结果:

八、读取所有的表单参数

以下是使用HttpServletRequest的getParameterNames()方法的通用实例来读取所有可用的表单参数。该方法返回一个枚举,包含了未指定顺序的参数名称。

一旦得到一个枚举,可以以标准方式循环这个枚举,使用hasMoreElements()方法来确定何时停止循环,使用nextElement()方法来获取每个参数的名称。

// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
// Extend HttpServlet class
public class ReadParams extends HttpServlet {// Method to handle GET method request.public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{// Set response content typeresponse.setContentType("text/html");PrintWriter out = response.getWriter();String title = "Reading All Form Parameters";String docType ="<!doctype html public \"-//w3c//dtd html 4.0 " +"transitional//en\">\n";out.println(docType +"<html>\n" +"<head><title>" + title + "</title></head>\n" +"<body bgcolor=\"#f0f0f0\">\n" +"<h1 align=\"center\">" + title + "</h1>\n" +"<table width=\"100%\" border=\"1\" align=\"center\">\n" +"<tr bgcolor=\"#949494\">\n" +"<th>Param Name</th><th>Param Value(s)</th>\n"+"</tr>\n");Enumeration paramNames = request.getParameterNames();      while(paramNames.hasMoreElements()) {String paramName = (String)paramNames.nextElement();out.print("<tr><td>" + paramName + "</td>\n<td>");String[] paramValues =request.getParameterValues(paramName);// Read single valued dataif (paramValues.length == 1) {String paramValue = paramValues[0];if (paramValue.length() == 0)out.println("<i>No Value</i>");elseout.println(paramValue);} else {// Read multiple valued dataout.println("<ul>");for(int i=0; i < paramValues.length; i++) {out.println("<li>" + paramValues[i]);}out.println("</ul>");}}out.println("</tr>\n</table>\n</body></html>");}// Method to handle POST method request.public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}

配置web.xml:

    <servlet><servlet-name>ReadParams</servlet-name><servlet-class>com.jsoft.testservletbasics.ReadParams</servlet-class></servlet><servlet-mapping><servlet-name>ReadParams</servlet-name><url-pattern>/ReadParams</url-pattern></servlet-mapping>

现在,用下面的表单尝试上述Servlet:

<html>
<body>
<form action="ReadParams" method="POST" target="_blank">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics"  /> Physics
<input type="checkbox" name="chemistry" checked="checked" /> Chem
<input type="submit" value="Select Subject" />
</form>
</body>
</html>

现在使用上述表单调用Servlet将产生如下所示结果:

 

测试工程:https://github.com/easonjim/5_java_example/tree/master/servletbasics/test2

==>如有问题,请联系我:easonjim#163.com,或者下方发表评论。<==
http://www.lbrq.cn/news/1081549.html

相关文章:

  • wordpress webfont/热狗网站关键词优化
  • wordpress po翻译/谷歌seo优化
  • 在洪雅网站做企业招聘/网页制作公司排名
  • 两学一做网站注册/百度一下百度网页版进入
  • 有哪些做短租的网站好/关键词排名优化
  • wordpress快速仿站/搜索引擎广告形式有
  • 安卓ui用什么软件设计/怎样优化网站
  • 企业移动网站建设/seo教程seo官网优化详细方法
  • 时间轴网站设计/微软优化大师
  • 深圳做网站要多少钱/线上营销方式
  • 网站注册平台/十大广告公司排名
  • 网站的优化方案/肇庆seo排名
  • WordPress如何建小语种网站/百度云盘登录入口
  • 黔江网站制作/seo黑帽教学网
  • 百度文库推广网站/软文推广策划方案
  • 重庆网站建设模板服务/佛山快速排名
  • wordpress/woocommerce/seo广告投放
  • 青岛网站建设有哪些公司/外链管理
  • 网站建设与维护 电子版/电脑培训班多少费用
  • 公司网站建设需要提供什么材料/每天看七个广告赚40元的app
  • 自己做的网站怎么设置地址/全国十大婚恋网站排名
  • 做网站推广对电脑有什么要求/超级推荐的关键词怎么优化
  • 古交网站建设/新品上市怎么推广词
  • 白银市网站建设/如何做品牌推广方案
  • 自做视频网站赚钱吗/完美日记网络营销策划书
  • 东莞做网站要多少钱/朋友圈推广
  • 做婚恋交友网站模板/线上推广有哪些渠道
  • 网站建设售后服务合同/国内最新的新闻
  • 56m做图片视频的网站是什么/热点新闻事件素材
  • 东莞长安/武汉网站seo
  • 【生活篇】Ubuntu22.04安装网易云客户端
  • 构型空间(Configuration Space,简称C-space)
  • LLC电源原边MOS管DS增加RC吸收对ZVS的影响分析
  • Python 程序设计讲义(45):组合数据类型——集合类型:集合的常用操作
  • 【Qt】QTime::toString(“hh:mm:ss.zzz“) 显示乱码的原因与解决方案
  • 如何在出售Windows11/10/8/7前彻底清除电脑数据