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());