您的位置:首页 > 编程语言 > Java开发

java总结篇—Servlet服务器

2016-08-21 22:12 253 查看
在学习的过程中,搭建服务器主要的目的应该是为了给自己提供接口抓数据,方便自己的学习。那下面简单的叙述搭建服务器的步骤。

搭建简易服务器其中一种方式:

1:可以下载Tomcat免费服务器

2:创建Dynamic Web Project项目(在javaee模式下)

3:新建Servlet类

4:自定义Servlet的doGet和doPost请求。

完成以上步骤就能够搭建一个简易的服务器啦,就能够被网页端或者客户端请求数据了。

*如果没有自动导入Tomcat包的话可以右键项目-build-path-configure build path - libarary - add libarary - Server runtime ...应该就可以了。有时候去导入被人项目的时 候httpServlet会报错就是因为这个包没了,执行上面的步骤也可以解决,

*还有就是有些可能是电脑的原因吧,需要把Tomcat中的lib目录下的servlet.jar包复制粘贴到Web Content - Web-Inf - lib 目录下(例如我的就需要这样的,但我的同学不需要也能正常运行)。

那下面主要说下doGet与doPost方法重写,这才是整个过程最重要的地方。

doGet():

特点:

不安全,因为请求的参数追加在URL上,没加密的话就会被直接看到而且追加的长度是有限制的,且由于URL采用的是ISO8859-1的编码格式,所以中文会乱码。

但速度很快,所以实际的应用中大多数还是doget的请求

方法:protected void doGet(HttpServletRequest request, HttpServletResponse response)

方法中接受两个参数:request(请求)和response(响应)

request:

包含着请求的信息。并提供一些方法使用:

*getParamaeter(?)获得传过来的值

*getRemoteAddr() 获得请求的ip

*setCharacterEncoding() 设置请求的编码格式

*getInputStream() 获取请求的对象流

*response是响应参数。常用方法

*setCharacterEncoding()设置响应编码格式

*getWriter() 获取相应输出流

*setState() 设置相应状态码

插入知识点——响应状态码:

1开头:消息

2开头:成功 200:OK,206:部分响应成功

3开头:重定向

4开头:请求错误 404:找不到页面

5开头:服务器错误 500:服务器程序崩溃

所以客户端在请求服务器的时候一般都会验证请求码是否为200即请求成功再操作。

那,是时候上代码了。

自定义的服务器:

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

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

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接受两个传过来的参数,姓名跟年龄,
String name = request.getParameter("name");//中文
String age = request.getParameter("age");//数字
response.setCharacterEncoding("UTF-8");//设置响应编码格式
PrintWriter pw = response.getWriter();
//request.setCharacterEncoding("UTF-8");在doget方法内设置请求编码格式的意义不大
if(name!=null&&name.length()!=0&&age!=null&&age.length()!=0){//验证是否有参数传过来
name = new String(name.getBytes(),"UTF-8");//将url的编码格式转成UTF-8
pw.write("响应内容:你输入了-姓名:"+name+"年龄:"+age);//输出响应的内容
}
}

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

}
使用httpurlconnection来进行Get请求:

public class Client_Main1 {
public static void main(String args[]) throws IOException{
String path = "http://localhost:8080/Servlet/MyServlet1?name=梁&age=22";
URL url = new URL(path);                                         //1.创建url对象
HttpURLConnection huc = (HttpURLConnection) url.openConnection();//2.通过url对象创建HttpURLConnection对象
huc.setConnectTimeout(3000);                                     //3.设置信息
huc.connect();                                                   //4.连接
if(huc.getResponseCode()==200){<span style="white-space:pre">					</span> //5.验证响应状态
BufferedInputStream bis = new BufferedInputStream(huc.getInputStream());//6.获取相应内容
int len ;
byte[] data = new byte[1024];
while((len=bis.read(data))!=-1){
System.out.println(new String(data,0,len));
}
bis.close();
huc.disconnect();
}
}
}
记住请求的6个步骤就好办了。

最后在控制台输出的结果:

响应内容:你输入了-姓名:梁年龄:22

dopost的方法写法跟doGet都是差不多的这里就不再写了。

就到这结束了。。。生活愉快!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息