Day8.12
2015-08-13 19:20
218 查看
DoGet方法
每个Servlet一般都需要重写doGet方法,因为父类的HttpServlet的doGet方法是空的,没有实现任何代码,子类需要重写此方法。doGet方法的定义代码如下:public void doGet(HttpServletRequest request,HttpServletResponse response )throws
ServletException,IOException{
}
当客户使用GET方式请求Servlet时,Web容器调用doGet方法处理请求。[1]
一般来说我们是用不到doGet方法的,doGet方法提交表单的时候会在url后边显示提交的内容,所以不安全。而且doGet方法只能提交256个字符(1024字节),而doPost没有限制,因为get方式数据的传输载体是URL(提交方式能form,也能任意的URL链接),而POST是HTTP头键值对(只能以form方式提交)。通常我们使用的都是doPost方法,你只要在servlet中让这两个方法互相调用就行了,例如在doGet方法中这样写:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
再把业务逻辑直接写在doPost方法中。servlet碰到doGet方法调用直接就会去调用doPost因为他们的参数都一样。而且doGet方法处理中文问题很困难,要写过滤器之类的。
public void actionPerformed(ActionEvent arg0) { String urlString="http://localhost:8080/MyTest/MyTestServlet?username=小明&password=123456";//获取url HttpClientBuilder builder=HttpClientBuilder.create(); builder.setConnectionTimeToLive(3000, TimeUnit.MILLISECONDS); //生成client的builder HttpClient client=builder.build(); //生成client HttpGet get=new HttpGet(urlString);//设置为GET方法 get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); try { HttpResponse response=client.execute(get);//执行get方法得到的服务器返回的所有数据都在response中 StatusLine statusLine=response.getStatusLine();//http访问服务器返回的表头,包含http状态码 int code=statusLine.getStatusCode(); if(code==HttpURLConnection.HTTP_OK){ HttpEntity entity=response.getEntity();//得到数据实体 InputStream is=entity.getContent(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String line=br.readLine(); if(line!=null){ System.out.println(line); line=br.readLine(); } }
DoPOST方法
DoPost是隐式的,相对doget安全且提交内容大小没有限制。public void actionPerformed(ActionEvent arg0) { String urlString="http://localhost:8080/MyTest/MyTestServlet"; try { URL url=new URL(urlString);//获取url HttpClientBuilder builder=HttpClientBuilder.create(); builder.setConnectionTimeToLive(3000, TimeUnit.MILLISECONDS); HttpClient client=builder.build(); HttpPost post=new HttpPost(urlString); NameValuePair pair1=new BasicNameValuePair("username", "小明"); NameValuePair pair2=new BasicNameValuePair("password", "123456"); ArrayList<NameValuePair> params=new ArrayList<>(); params.add(pair1); params.add(pair2); post.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));//网络编码形式实体 post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); HttpResponse response=client.execute(post); int code=response.getStatusLine().getStatusCode(); if(code==200){ HttpEntity entity=response.getEntity(); InputStream is=entity.getContent(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String line=br.readLine(); while(line!=null){ System.out.println(line); line=br.readLine(); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
实现Servlet与数据库的连接
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class MySqlMannager { //单例模式 private static MySqlMannager mannager; private Statement statement; private Connection connection; public Connection getConnection() { return connection; } public void setConnection(Connection connection) { this.connection = connection; } public Statement getStatement() { return statement; } public static synchronized MySqlMannager newInstance(){ if(mannager==null){ mannager=new MySqlMannager(); } return mannager; } public void setStatement(Statement statement) { this.statement = statement; } private MySqlMannager(){ String driver="com.mysql.jdbc.Driver";//连接数据库的驱动 String url="jdbc:mysql://localhost:3306/clazz";//URL指向要访问的数据库名 String user="root"; //MYSQL的用户名 String password="111111";//MySQL密码 try { Class.forName(driver);//加载驱动 connection = DriverManager.getConnection(url, user, password);// 连接数据库 // statement = connection.createStatement(); // String create = "CREATE TABLE USER(id int(11) not null PRIMARY KEY auto_increment,user_name varchar(30) not null,password varchar(30) not null)"; } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 加载驱动 catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- poj 1637 Sightseeing tour 混和欧拉图 dinic
- hdu3371 Connect the Cities (MST)
- poj 1651 Multiplication Puzzle 矩阵链乘问题
- Android闹钟
- poj 1637 Sightseeing tour 混和欧拉图 dinic
- Redis学习(2)-数据类型String和Hashes
- 超链接收集
- 如何解决秒杀的性能问题和超卖的讨论
- python爬虫
- Oracle查询结果输出问题
- cocos3.x导出自定义类到Lua以及手动绑定要回调的函数
- 简析Android mediaPlayer 播放在线音乐网络异常状态的处理
- Linux下Android studio 创建项目build错误
- HDU 3117 Fibonacci Numbers
- POJ 3026 Borg Maze
- RelativeLayout用到的一些重要的属性:
- Java学习之路0813(HttpClient中的doGet和doPost实例)
- C++ 友元(共享数据)
- MVP模式解读与实战运用(二)
- Cocos2d-x 2.3.3版本 FlappyBird