Serverlet搭建

1、解压apache-tomcat-7.0.63-windows-x64.zip文件

2、关闭eclipse,解压tomcatPluginV331.zip文件,将解压出来的

com.sysdeo.eclipse.tomcat_3.3.1.jar的jar包复制到eclipse安装路径中的plugins文件夹下,C:

\software\java\eclipse\plugins
3、打开eclipse软件可以发下如下图所示的三个小图标
这里写图片描述

4、点击window-preference-Tomcat-Tomcat home处找到解压的apache文件夹选中ok

5、选择图标Java EE,并在左侧空白处单击,选择new-other-Web-Dynamic Web Project-Next-输入名字,在targetruntime处选择Apache tomcat7.0,next-next,在最后处记得勾选选项。
Serverlet搭建_第1张图片

6、buildpath-config-AddExternjar-browser-找到apache-tomcat-7.0.63-windows-x64.zip的解压文件夹apache-tomcat-7.0.63-lib-serverlet.jar
7、点击Web Content-new-HTML File新建一个index.html文件。index.html文件样本如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>百度一下,你就知道</title>
</head>
<body>
<a>HelloWorld.html</a>
<p>This is My first WEB</p>
</body>
</html>

8、在java Resource下建立包,在包下建立new-serverle文件Myserverlettest
Serverlet搭建_第2张图片
Myserverlettest的大致内容如下(其中有修改部分)

package com.lingzhuo;

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 Myserverlettest */
@WebServlet("/Myserverlettest")
public class Myserverlettest extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        username=Encoding.doEncoding(username);//导包
        System.out.println(""+username+":"+password);
        String s="得到的用户名:"+username+":"+password;
        response.setHeader("Content-type","text/html;charset=UTF-8");
        response.getWriter().append(s);


    // response.getWriter().append("Served at: ").append("用户名"+username+"密码"+password);
    }

    /** * @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);
    }

}

9、为获得正确的中文字符而不是乱码,进行再次编码,在同包下建立java class文件Encoding。
注:在eclipse下也需要修改,单击文件名Myservicetest-properties
Serverlet搭建_第3张图片

package com.lingzhuo;

import java.io.UnsupportedEncodingException;

public class Encoding {
    public static String doEncoding(String string){

        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;
    }

}

注:关于乱码:

1、使用浏览器提交数据默认的编码格式为ISO-8859-1
2、得到String username它的编码格式为8859-1线使用ISO-8859-1转化为字节数组
3、再使用utf-8编码格式将字节数组转化为字符串

关于浏览器:有时在进行了编码的转换之后仍不能正常显示时可以换一个浏览器尝试。
http://localhost:8080/MyServiceTest/Myserverlettest?username=张三&password=123456

补充:使用tomcat搭建后台服务器,主要做servelet为前段提供数据,doGet在网址后边跟上要提交的数据

你可能感兴趣的:(Serverlet搭建)