Servlet的请求HttpServletRequest
一、从容器到HttpServlet
1、web容器作了什么
web容器做的事情就是,创建Servlet实例,并完成Servlet的名称注册及URL模式的对应。在请求来到时,web容器会转发给正确的Servlet来处理请求。
当请求来到http服务器时,而http服务器转交请求给容器时,容器会创建一个代表档次请求的HttpServletRequest对象,并将请求相关信息设置给该对象。同时,容器会创建一个HttpServletResponse对象,作为稍后要对客户端进行相应的java对象。
接着,容器会根据读取的@webServlet标注或者xml的设置,找出处理该请求的servlet,调用它的service()方法,将创建的HttpServletRequest对象、HttpServletResponse对象传入作为参数,service()方法中会根据HTTP请求的方式,调用对应的doXXX()方法。接着再doXXX()方法中,可以使用request、resposne对象,如:getParameter()取得请求参数,使用getWriter()取得输出用的PrintWriter对象,并进行各项响应处理。对PrintWriter做的输出操作,最后由容器转换为HTTP响应,再由HTTP服务器对浏览器响应。之后容器将对象销毁。
2、关于HttpServletRequest
2.1 处理请求参数与标头
String username = request.getParameter("name");指定请求参数名称来取得对应的值。如果传来的是"123"这样的字符串值,必须使用Integer.parseInt()这类方法剖析为基本类型。
String[] values = request.getParameterValues("param");就像param=10¶m=20¶m=30,此时用getParameterValues()取得一个String数组。
如果想要知道请求中有多少请求参数,则可以使用getParameterNames()方法,会返回一个Enumeration对象。
Enumeration<String> e = request.getParameterNames();
while(e.hasMoreElements()){
String param = e.nextElement();
...
}
对于HTTP标头的信息(Header),可以通过以下方法取得:
1、getHeader():使用方式与getParameter类似。
2、getHeaders():与getParameterValues类似。
3、getHeaderNames():与getParameterNames类似。
以下的实例如何取得并显示浏览器送出的标头信息。
package cc.openhome; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration; 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 HeaderServlet
*/
@WebServlet("/header.view")
public class HeaderServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public HeaderServlet() {
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
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>HeadServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>HeaderServlet at "+request.getContextPath()+"</h1>");//取得应用程序环境路径
Enumeration<String> names=request.getHeaderNames();//取得所有标头名称
while(names.hasMoreElements()){
String name=names.nextElement();
out.println(name+": "+request.getHeader(name)+"<br>");//输出所有的标头值
}
out.println("</body>");
out.println("</html>");
out.close();
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }
HeaderServlet
运行结果:
2.2 getReader()、getInputStream()读取Body内容
getReader()方法,可以取得一个BufferedReader对象,可以读取Body数据。
例子1:读取请求Body内容
package cc.openhome; import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter; 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 BodyServlet
*/
@WebServlet("/body.view")
public class BodyServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public BodyServlet() {
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 } private String readBody(HttpServletRequest request) throws IOException {
// TODO Auto-generated method stub
BufferedReader reader=request.getReader();//取得BufferedReader对象
String input=null;
String requestBody="";
while((input=reader.readLine())!=null){
requestBody=requestBody+input+"<br>";
}
return requestBody;
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String body=readBody(request);
PrintWriter out=response.getWriter();//取得响应输出对象
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet BodyView</title>");
out.println("</head>");
out.println("<body>");
out.println(body);
out.println("</body>");
out.println("</html>");
out.close();
} }
BodyServlet
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="body.view" method="post">
名称:<input type="text" name="user"><br>
密码:<input type="password" name="passwd"><br><br>
<input type="submit" name="login" value="送出">
</form>
</body>
</html>
form.html
在名称字段上输入“良葛格”,密码上输入“123456”,单机送出按钮,得到:
2.3 getPart()、getParts()取得上传文件
Servlet3.0中,新增了Part接口,可以方便进行文件上传处理。可以通过request对象的getPart()方法取得Part对象。
例1:上传文件到指定目录。
package cc.openhome; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part; /**
* Servlet implementation class PhotoServlet
*/
@MultipartConfig(location="e:/java web/workspace/")//设置location属性
@WebServlet("/photo.do")
public class PhotoServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public PhotoServlet() {
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
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
Part part=request.getPart("photo");
String filename=getFilename(part);
//writeTo(filename,part);
part.write(filename);//将文件写入location的指定目录
} // private void writeTo(String filename, Part part) throws IOException {
// // TODO Auto-generated method stub
// InputStream in=part.getInputStream();
// OutputStream out=new FileOutputStream("e:/java web/workspace/"+filename);
// byte[] buffer=new byte[1024];
// int length=-1;
// while((length=in.read(buffer))!=-1){
// out.write(buffer,0,length);
// }
// in.close();
// out.close();
// } private String getFilename(Part part) {
// TODO Auto-generated method stub
String header=part.getHeader("Content-Disposition");
String filename=header.substring(header.indexOf("filename=\"")+10,header.lastIndexOf("\""));
return filename; } }
UploadServlet.java
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<title>Insert title here</title>
</head>
<body>
<form action="photo.do" method="post" enctype="multipart/form-data">
上传相片:<input type="file" name="photo" value="" /><br>
<input type="submit" name="upload" value="上传">
</form>
</body>
</html>
upload.html
在Tomcat中必须设置@MultipartConfig标注才能使用getPart()相关API。@MultipartConfig也可以设置属性,比如location。@MultipartConfig(location="c:/workspace"),则上例子就可以修改为:
package cc.openhome; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part; /**
* Servlet implementation class PhotoServlet
*/
@MultipartConfig(location="e:/java web/workspace/")//设置location属性
@WebServlet("/photo.do")
public class PhotoServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public PhotoServlet() {
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
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
Part part=request.getPart("photo");
String filename=getFilename(part);
//writeTo(filename,part);
part.write(filename);//将文件写入location的指定目录
} // private void writeTo(String filename, Part part) throws IOException {
// // TODO Auto-generated method stub
// InputStream in=part.getInputStream();
// OutputStream out=new FileOutputStream("e:/java web/workspace/"+filename);
// byte[] buffer=new byte[1024];
// int length=-1;
// while((length=in.read(buffer))!=-1){
// out.write(buffer,0,length);
// }
// in.close();
// out.close();
// } private String getFilename(Part part) {
// TODO Auto-generated method stub
String header=part.getHeader("Content-Disposition");
String filename=header.substring(header.indexOf("filename=\"")+10,header.lastIndexOf("\""));
return filename; } }
UploadServlet.java
如果有多个文件要上传,可以使用getParts方法,这回返回一个Collection<Part>,其中是每个上传文件的part对象。
例子2:上传多个文件
package cc.openhome; import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException; 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 UploadServlet
*/
@WebServlet("/upload.do")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
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
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
//锟斤拷取锟斤拷锟斤拷Body
byte[] body=readBody(request);
//取锟斤拷锟斤拷锟斤拷Body锟斤拷锟捷碉拷锟街凤拷锟斤拷锟斤拷示
String textBody=new String(body,"UTF-8");
//取锟斤拷锟较达拷锟斤拷锟斤拷锟斤拷锟侥硷拷
String filename=getFilename(textBody);
//取锟斤拷锟侥硷拷锟斤拷始锟斤拷锟斤拷锟轿伙拷锟�
Position p=getFilePosition(request,textBody);
//锟斤拷锟斤拷锟斤拷募锟�
writeTo(filename,body,p);
} private void writeTo(String filename, byte[] body, Position p) throws IOException {
// TODO Auto-generated method stub
FileOutputStream fileOutputStream=new FileOutputStream("e:/java web/workspace/"+filename);
fileOutputStream.write(body,p.begin,(p.end-p.begin));
fileOutputStream.flush();
fileOutputStream.close();
} private Position getFilePosition(HttpServletRequest request, String textBody) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
//取得实际上传的文件
String contentType=request.getContentType();
String boundaryText=contentType.substring(contentType.lastIndexOf("=")+1,contentType.length());
//取得实际上传的文件
int pos=textBody.indexOf("filename=\"");
pos=textBody.indexOf("\n",pos)+1;
pos=textBody.indexOf("\n",pos)+1;
pos=textBody.indexOf("\n",pos)+1;
int boundaryLoc=textBody.indexOf(boundaryText,pos)-4;
int begin=((textBody.substring(0,pos)).getBytes("GBK")).length;
int end=((textBody.substring(0,boundaryLoc)).getBytes("GBK")).length;
return new Position(begin, end);
} private String getFilename(String textBody) {
// TODO Auto-generated method stub
String filename=textBody.substring(textBody.indexOf("filename=\"")+10);
filename=filename.substring(0,filename.indexOf("\n"));
filename=filename.substring(filename.lastIndexOf("\\")+1,filename.indexOf("\""));
return filename;
} private byte[] readBody(HttpServletRequest request) throws IOException {
// TODO Auto-generated method stub
int formatDataLength=request.getContentLength();
DataInputStream dataStream=new DataInputStream(request.getInputStream());//取锟斤拷InputStraem锟侥讹拷锟斤拷
byte body[]=new byte[formatDataLength];
int totalBytes=0;
while(totalBytes<formatDataLength){
int bytes=dataStream.read(body,totalBytes,formatDataLength);
totalBytes+=bytes;
}
return body;
} }
class Position{
int begin;
int end;
public Position(int begin,int end) {
// TODO Auto-generated constructor stub
this.begin=begin;
this.end=end;
}
}
UploadServlet2.java
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<title>Insert title here</title>
</head>
<body>
<form action="upload1.do" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="file1" value="" /><br>
文件2:<input type="file" name="file2" value="" /><br>
文件3:<input type="file" name="file3" value="" /><br><br>
<input type="submit" name="upload" value="上传"/>
</form>
</body>
</html>
multi-upload.html
2.4 使用RequestDispatcher调派请求
在web应用程序中,经常需要多个Servlet来完成请求。例如,将另一个Servlet的请求处理流程包含进来,或者将请求转发forward给别的servlet处理。可以使用:
RequestDispatcher dispatcher = request.getRequestDispatcher("some.do");
(1)、使用include()方法
将另一个servlet的操作流程包括至目前的servlet操作之中。
例1:include方法
package cc.openhome; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.RequestDispatcher;
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 Some
*/
@WebServlet("/some.view")
public class Some extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public Some() {
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
PrintWriter out=response.getWriter();
out.println("Some do one...");
RequestDispatcher dispatcher=request.getRequestDispatcher("other.view?data=123456");
dispatcher.include(request, response);
out.println("Some do two...");
out.close();
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }
some.java
package cc.openhome; import java.io.IOException;
import java.io.PrintWriter; 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 OtherServlet
*/
@WebServlet("/other.view")
public class OtherServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public OtherServlet() {
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
PrintWriter out=response.getWriter();
out.println("Other do one..");
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }
othersome.java
运行结果:网页上看到响应顺序是Some do one ... Other do one... Some do two.. .
(2)、使用forward()方法
将处理请求转发给别的Servlet。
例2:forwod方法
package cc.openhome; 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; import com.sun.javafx.sg.prism.NGShape.Mode;
import com.sun.media.sound.ModelAbstractChannelMixer; /**
* Servlet implementation class HelloController
*/
@WebServlet("/hello.do")
public class HelloController extends HttpServlet {
private static final long serialVersionUID = 1L;
private HelloModel model=new HelloModel();
/**
* @see HttpServlet#HttpServlet()
*/
public HelloController() {
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 name=request.getParameter("user");//收集请求参数
String message=model.doHello(name);//委托HelloModel对象处理
request.setAttribute("message", message);//将结果信息设置至请求对像成属性
request.getRequestDispatcher("hello1.view").forward(request, response);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub } }
HelloController.java
package cc.openhome; import java.util.*; public class HelloModel {
private Map<String, String>messages=new HashMap<String,String>();
public HelloModel(){
messages.put("caterpillar", "Hello");
messages.put("Justin", "Welcome");
messages.put("momor", "Hi");
}
public String doHello(String user){
String message=messages.get(user);
return message+", "+user+"!";
}
}
HelloModel.java
package cc.openhome; 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 HelloView
*/
@WebServlet("/hello1.view")
public class HelloView extends HttpServlet {
private static final long serialVersionUID = 1L;
private String htmlTemplate=
"<html>"
+ "<head>"
+ "<meta http-equiv='Content-Type'"
+ "content='text/html; charset=UTF-8'>"
+ "<title>%s</title>"
+ "</head>"
+ "<body>"
+ "<h1>%s</h1>"
+ "</body>"
+"</html>";
/**
* @see HttpServlet#HttpServlet()
*/
public HelloView() {
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 user=request.getParameter("user");//取得请求参数
String message=(String) request.getAttribute("message");//取得请求属性
String html=String.format(htmlTemplate, user,message);//产生html结果
response.getWriter().print(html);//输出html结果 } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
} }
HelloView.java
运行结果:(Model2架构)
Servlet的请求HttpServletRequest的更多相关文章
- Filter 快速开始 异步Servlet 异步请求 AsyncContext 异步线程 异步派发 过滤器拦截
[web.xml] <filter> <filter-name>normalFilter</filter-name> <filter-class>net ...
- JAVA记录-Servlet RequestDispatcher请求转发
RequestDispatcher接口提供将请求转发送到另一个资源的功能,它可能是html,servlet或jsp等. 此接口也可用于包括另一资源的内容.它是servlet协作的一种方式. 在Requ ...
- Servlet 处理请求
一:Servlet 处理请求也是通过request来进行处理,类似于python. get请求通过request.getparameter("key");key为前端传过来的key ...
- servlet单例模式以及servlet的请求流程
目录 一.servlet 单例模式: 二.servlet的请求流程: ★ 流程: ★ 详细过程如下: 一.servlet 单例模式: tomcat服务器,通过Java的反射机制来自动调用servl ...
- 动手学servlet(三) 请求头和响应头信息
获取请求头信息 package servletdemo; import java.io.IOException; import java.util.Enumeration; import javax. ...
- 使用Servlet处理请求<http://blog.sina.com.cn/s/blog_5d3fb3cc0100ep9q.html>
一.GET和POST的区别 1.GET提交的是文本内容,规定其数据长度不超过255个字符.在GET方式提交的URL中会显示出提交的查询数据而却提交数据的缓存会在浏览器的URL历史状态中,这样我们往往在 ...
- JavaWeb之Servlet:请求 与 响应
1 引入 浏览器和服务器的种类都有很多,要在它们之间通讯,必定要遵循一定的准则,而http协议就是这样的一个"准则". Http协议:规定了 浏览器 和 服务器 数据传输的一种格式 ...
- [02] Servlet获取请求和页面跳转
1.Tomcat和Servlet的关系 之前提到过,Servlet是运行在Web容器里的,Tomcat作为容器的一种,在这里自然也要大概说说两者之间的大致关系. 首先,如上所述,Tomcat是Web应 ...
- Servlet 转发请求与重定向,以及路径问题
转发请求 当一个servlet接收到请求后,如果需要将请求转发给另外一个servlet或者jsp文件,可使用下面这种方法: package cn.ganlixin.servlet; import ja ...
随机推荐
- vpn探测
root@bt:~# ike-scan -M -v 192.168.1.102 root@bt:~# sslscan 192.168.1.102
- ajax编程**
ajax 编程 *step1获得 XmlHttpRequest 对象.该对象由浏览器提供,但是该类型并没有标准化.ie 和其它浏览器不同,其它浏览器都支持该类型,而 ie 不支持. function ...
- Java之Property类使用
http://blog.csdn.net/hopestar2/article/details/6372883 在项目中经常用到各种配置文件,有.properties的,也有.xml格式的 都可以通过j ...
- Firebug Console 与命令行全集
Console API 当打开 firebug (也包括 Chrome 等浏览器的自带调试工具),window 下面会注册一个叫做 console 的对象,它提供多种方法向控制台输出信息,供开发人员调 ...
- Linux:网络yum源设置
网络yum源,最大的网易(163)算是一个,无论是速度还是软件版本,都非常的不错,将yum源设置为163yum,可以提升软件包安装和更新的速度,同时避免一些常见软件版本无法找到.具体设置方法如下: 1 ...
- 丑数 LeeTCode
题目链接:http://www.lintcode.com/zh-cn/problem/ugly-number-ii/ 题目描述:设计一个算法,找出只含素因子2,3,5 的第 n 大的数.符合条件的数如 ...
- 17.4.3 使用MulticastSocket实现多点广播(5)
该类主要实现底层的网络通信功能,在该类中提供了一个broadCast()方法,该方法使用Multicast Socket将指定字符串广播到所有客户端:还提供了sendSingle()方法,该方法使用D ...
- iOS之UILabel自适应大小
//初始化一个label self.label=[[UILabel alloc] init]; //设置自动行数与字符换行 [self.label setNumberOfLines:0]; //给la ...
- STM32之FreeRTOS
STM32之FreeRTOS http://www.freertos.org/index.html http://www.freertos.org/a00090.html#ST http://www. ...
- kafka第六篇---多个进程读不齐
问题: 4台kafka组成的集群,多余5个进程读的话,造成偏移不齐. 解决办法: 用小于4个进程执行,如果执行够快,就可以追得上