dynamic-- web的创建

1.解压tomcatPluginV331压缩包,把里面的文件拷贝到eclipse的目录下示例(D:\eclipse-jee-mars-R-win32\eclipse\plugins),然后启动eclipse,在上方出现小猫
2.解压apache-tomcat-7.0.63-windows-x64,然后放到自己想放的目录,然后在windows下的prefer下的tomcat填入tomcat的目录,ok
3.在右上角点击 javaEE,然后创建工程,选web下dynamic– web创建工程
创建一个Html文件,nihao,然后run on server

4.创建一个新包com.web.test,然后创建一个sevlet,然后去tomcat下用build path导入sevlet-api.jar

注意在网页中访问时添加@WebServlet(“/webproject”)引号中的内容
在网页中传入内容的方法http://localhost:8080/MySeverTest/webproject?username=zhansan

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    //request为网页传入服务器的内容,respose从服务器传递给客户端
        System.out.println(request.getParameter("username"));
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

得到String username的编码格式为ISO-8859-1
先使用8859-1转化成字节数组,
再使用utf-8编码格式把字节数组转化成字符串

//自己添加的转换类
import java.io.UnsupportedEncodingException;

public class Encoding {
public static String doEncoding(String string){
 if( string==null){  //解决空指针的问题
        return null;
    }
    try {
        byte [] array=string.getBytes("ISO-8859-1");
        string=new String(array,"UTF-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return string;
}

//在servlet中的使用
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;

/**
 * Servlet implementation class MyTest
 */
@WebServlet("/MyTest")
public class MyTest extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyTest() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String userName=request.getParameter("username");
    String password=request.getParameter("password");
    userName=Encoding.doEncoding(userName);
    System.out.println(""+userName+":"+password);
//  try {
//      Thread.sleep(10000);
//  } catch (InterruptedException e) {
//      // TODO Auto-generated catch block
//      e.printStackTrace();
//  }
    String s="得到的用户名为"+userName+":"+password;

    response.setHeader("Content-type", "text/html;charset=UTF-8");
    //让浏览器以UTF-8编码格式解析
    response.getWriter().append(s);
//      System.out.println(request.getParameter("username"));   
//  System.out.println(request.getParameter("password"));

        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}


//在网页中的提交数据
http://localhost:8080/WebTest/MyTest?username=zhangsan&password=12345

    "返回数据以中文格式显示则添加"
response.setHeader("Content-type","textml;charset=UTF-8");
        response.getWriter().append("我收到了: ").append(request.getContextPath());

doget与dopost的区别

  1. 当服务器创建servlet对象后,该对象会调用init方法初始化自己,以后每当服务器再接收到一个servlet请求时,就会产生一个新线程,在这个线程中让servlet对象调用service对象检查HTTP请求类型(get,post),并在service方法中根据请求类型对应的调用doGet ,doPost方法。

  2. 如果不论用户请求类型是get还是post,服务器处理过程完全相同,那么可只在doPost中编写处理过程,在doGet中调用doPost就行了,反之也可以doGet中处理,doPost中调用doGet。如果根据请求类型不同而需要不同的处理,就需要在两个方法中编写不同的处理过程。get是显式的、请求的东西(你页面输入的信息)会在地址栏显示所以不安全,post是隐式的、不显示,会好一点。一般用于表单的传递。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值