概述
实例1:
1.新建webproject2项目
对servlet进行申明,设置映射路径
2.新建LoginFormServlet.java
package com.jh.webproject2;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginFormServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginFormServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<form method="POST" action="/webproject2/servlet/LoginForm">");//"+request.getContextPath()+"可替代/webproject2
out.println("用户名:<input type='text' name='username'><br>");
out.println("密码:<input type='password' name='password'><br>");
out.println("<input type='submit' value='提交'>");
out.println("<input type='reset' value='重置'>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("utf-8");
out.println("用户名:"+request.getParameter("username")+"<br>");
out.println("密码:"+request.getParameter("password")+"<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
3.解决表单中文参数乱码
提交的时候是以 的码提交的,所以输出的时候会出现乱码的情况,所以在接收参数之前要进行转码
request.setCharacterEncoding("utf-8");
实例2:(在实例1的基础上的改进)
1.新建html文件
2.新建LoginServlet.java
重写doPost方法,不需要doGet方法
package com.jh.webproject2;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("utf-8");
out.println("用户名:"+request.getParameter("username")+"<br>");
out.println("密码:"+request.getParameter("password")+"<br>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
3. 修改路径
实例3:网络调查表
1.新建survey.htm
2.新建SurveyServlet.java
使用doGet,doPost方法,实际上主要处理的是doPost请求
package com.jh.webproject2;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SurveyServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public SurveyServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("utf-8");
out.println("姓名:"+filterHtml(request.getParameter("name"))+"<br>");
out.println("Email:"+filterHtml(request.getParameter("email"))+"<br>");
out.println("年龄:"+request.getParameter("age")+"岁"+"<br>");
out.println("编程时间:"+request.getParameter("codetime")+"个月"+"<br>");
out.println("操纵系统:");
String os[]=request.getParameterValues("os"); //枚举输出
out.println("<ul>");
for(int i=0;i<os.length;i++)
{
out.println("<li>"+os[i]+"</li>");
}
out.println("</ul><br>");
out.println("编程语言:");
String language[]=request.getParameterValues("language"); //枚举输出
out.println("<ul>");
for(int i=0;i<language.length;i++)
{
out.println("<li>"+language[i]+"</li>");
}
out.println("</ul><br>");
out.println("建议:"+filterHtml(request.getParameter("comment"))+"<br>");
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
//进行过滤
public String filterHtml(String value){
value=value.replaceAll("&", "&");
value=value.replaceAll("<", "<");//注:替换script代码,只须把代码中的"<"或">"号替换成普通文本输出就可以
value=value.replaceAll(">", ">");
value=value.replaceAll("", " ");
value=value.replaceAll("'", "'");
value=value.replaceAll(""", """);//这地方是替换双引号,""是对双引号进行转译
value=value.replaceAll("n", "<br>");
return value;
}
public void init() throws ServletException {
// Put your code here
}
}
3.对输入的东西进行过滤
使用filterHtml方法(具体实现方法在上面所列代码中)
************************************************
也是在本例中,学到一个小东西,是在每次上传文件时要用的东西一个浏览框:
<input type="file" value="浏览"/>很简单很实用吧。
实例4:在不知道所有参数的情况下,获取参数的名字以及参数所对应的值
1.新建survey2.htm
2.新建SurveyServlet2.java
//在不知道所有参数的情况下,获取参数的名字以及参数所对应的值
package com.jh.webproject2;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SurveyServlet2 extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 7799557625643493959L;
/**
* Constructor of the object.
*/
public SurveyServlet2() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
request.setCharacterEncoding("utf-8");
Enumeration e=request.getParameterNames();
String parameterName=null;
while(e.hasMoreElements()){ //枚举输出
parameterName=(String)e.nextElement();
String values[]=request.getParameterValues(parameterName);//得到每一个参数的值,通过下面的for循环输出
out.println(parameterName+"<br>");
out.println("<ul>");
for(int i=0;i<values.length;i++)
{
out.println("<li>"+values[i]+"</li>");
}
out.println("</ul><br>");
}
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public String filterHtml(String value){
value=value.replaceAll("&", "&");
value=value.replaceAll("<", "<");//注:替换script代码,只须把代码中的"<"或">"号替换成普通文本输出就可以
value=value.replaceAll(">", ">");
value=value.replaceAll("", " ");
value=value.replaceAll("'", "'");
value=value.replaceAll(""", """);//这地方是替换双引号,""是对双引号进行转译
value=value.replaceAll("n", "<br>");
return value;
}
}
转载于:https://www.cnblogs.com/java-zhu/archive/2008/08/19/1270722.html
最后
以上就是鲜艳小蝴蝶为你收集整理的servlet处理表单数据之实例开发(1)的全部内容,希望文章能够帮你解决servlet处理表单数据之实例开发(1)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复