servlet处理表单数据之实例开发(1)

实例1:
1.新建webproject2项目
 对servlet进行申明,设置映射路径
2.新建LoginFormServlet.java

 

ContractedBlock.gif ExpandedBlockStart.gif Code
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方法

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
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请求


 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
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("&""&amp;");
        value
=value.replaceAll("<""&lt;");//注:替换script代码,只须把代码中的"<"或">"号替换成普通文本输出就可以
        value=value.replaceAll(">""&gt;");
        value
=value.replaceAll("""&nbsp;");
        value
=value.replaceAll("'""'");
        value
=value.replaceAll("\"""&quot;");//这地方是替换双引号,"\"是对双引号进行转译
        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

 

ContractedBlock.gif ExpandedBlockStart.gif Code
//在不知道所有参数的情况下,获取参数的名字以及参数所对应的值
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("&""&amp;");
        value
=value.replaceAll("<""&lt;");//注:替换script代码,只须把代码中的"<"或">"号替换成普通文本输出就可以
        value=value.replaceAll(">""&gt;");
        value
=value.replaceAll("""&nbsp;");
        value
=value.replaceAll("'""'");
        value
=value.replaceAll("\"""&quot;");//这地方是替换双引号,"\"是对双引号进行转译
        value=value.replaceAll("\n""<br>");
        
return value;
    }
    

}

 

 

转载于:https://www.cnblogs.com/java-zhu/archive/2008/08/19/1270722.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值