servlet基礎知識 二

honghu79 13年前發布 | 2K 次閱讀 google應用商城
1、例子:
form.html文件抓包分析
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Form</title>
 </head>
 <body>
  <h1>
   表單
  </h1>
  <form action="go" method="get">
  <!--action,表示提交給誰;method表示用什么方式來提交一般使用post來提交-->
   <input type="text" name="user" value="java"/>
   <!--name的意義?將來這個表單提交的時候,name的值作為提交信息的名稱-->
   <input type="text" name="pwd" />
   <input type="password" name="pwd1"/>
   <!--type="password",在頁面上顯示時,輸入的內容變成了*號-->
   <input type="hidden" name="id" value="123" />
   <!--type,hidden,在頁面上不顯示-->
   <input type="submit" value="提交" />
  </form>
 </body>
</html>
其中meta    Content-type:text/html;charset=utf-8
 

全英文
user=memeory&abc=123&pwd=hello
      =    = 
memory&abc=123  
user=memeory%26abc%3D123&pwd=haha
        ==       ==
%跟一16進制的數據,表示一個字符
中文
user=%ED%BD%A0%ET%A5%BD
        ===================
  一個中文,一個utf8的編碼是3個字節,所以上面是2個字
  一個中文,gbk編碼是2個字節
2、form文件提交的數據 ,會以編碼的形式出現在文件中,就要看瀏覽器用什么的編碼打開頁面就用什么編碼來傳輸數據
服務器必須通過某種方法知道瀏覽器是用什么編碼方式提交的
 
URLEncoder.encode
URLDecoder.decode

3、servlet如何獲取表單的數據??
 
package org.whatisjava.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FormServlet extends HttpServlet{
 public void service(HttpServletRequest request,HttpServletResponse response)
     throws ServletException,IOException{
  //獲取請求協議中提交的文本數據
  //tomcat對于特殊字符默認用ISO 8859-1解碼
  //
  request.setCharacterEncoding("utf-8");
  //這句話必須寫在第一個getParameter之前
  
  String user=request.getParameter("user");
  String pwd=request.getParameter("pwd");
  
  System.out.println(user);
  System.out.println(pwd);
  
  
 }
}
 
4、練習
 
 表格
 添加一個新員工
 添加完成后出現新的員工列表,并返回對應的
 
 
5、Statement與 PreparedStatement的區別
 
Statement
 
 String user=request.getParameter("user");
 String pwd=request.getParameter("pwd");
 String url="";
 String username="";
 String password=""
 Class.forName("");
 Connection conn=DriverManager.getConnection(url,username,password);
 Statement stmt=conn.createStatement();
 
 ResultSet rs=stmt.executeQuery("
 select count(*) from t_user
 where user='"+user+"'   and pwd='"+pwd+"'
 ");
 //返回結果是1則存在,返回0則不存在
 
 采用以上方法是有問題的,那么是什么問題呢  ?
 
 假設 第一個文本框 輸入 hahaha,第二個 文本框  1'or'2'='2
 那么我們把其放入 以上 select語句中
 select count(*) from t_user where user='hahaha'  and pwd='1'or'2'='2'
 
 rs.next();
 if (rs.getInt(1)!=0){
 //登錄成功
 }
 
 SQL Injection!!!
 
PreparedStatement
 
 String user=request.getParameter("user");
 String pwd=request.getParameter("pwd");
 String url="";
 String username="";
 String password=""
 Class.forName("");
 Connection conn=DriverManager.getConnection(url,username,password);
 PreparedStatement ps=conn.createStatement("
 select count(*) from t_user
 where user=? and pwd=?
 ");
 ps.setString(1,user);
 ps.setString(2,pwd);
 ResultSet rs=ps.executeQuery();
 
PreparedStatement已經屏蔽了大多數的 sql注入攻擊的
6、重定向
   CLIENT   SERVER
 A----------->B第一次請求
 <------------   response里面帶有重定向的url  C
 ------------>C第二次請求
 <------------
 并且地址欄會發生變化成重定向后的地址
抓包分析:
第一次請求:
GET /serlvet02/redirect
...
Connection:keep-alive;

響應
HTTP/1.1 302 Moved Temporarily
Server:Apache=coyote/1.1
Location:
http://localhost:8080/servlet02/target.html
Content-Length:0
第二次請求:
GET /servlet02/taget.html
說明:
servlet只是通過api來生成重定向的位置
http協議本身就支持重定向
response.sendRedirect("target.html");  //重定向的location位置
7、
問題:web.xml在什么時候加載
web.xml在服務器服務啟動的時候就將該文件加載到了內存中
問題:一個頁面會有多個請求比如文本請求\圖片請求等
 
 
8、
如何讓服務器發送一個圖片到客戶端瀏覽器?
html文件中,加載一個圖片文件
<img src="img/tiger.jpg" width="200" height="260"/>
 
9、如何用程序動態生成圖片?
比如驗證碼圖片
swing畫圖
圖片在內存中是什么樣的?
是以位圖來呈現的,位圖以像素點個數,一個點的顏色個數
1個像素點=24位真彩圖=3個字節(3紅綠藍,1色8位),每顏色顏色值為0-255.3個色都為255則為白,都為0則為黑
3個字節24位=2的 24次方,1個像素就是24位(3個字節)
顏色數和像素數
 內存里面先可以生成一個點陣
  //設置響應類型
  response.setContentType("image/jpeg");
  
  BufferedImage image=new BufferedImage(60,20,BufferedImage.TYPE_INT_RGB);
  //可以封存圖片在內存中的點陣信息。圖片的內存影像
  //TYPE_INT_RGB   RGB,紅綠藍
  
  Random r = new Random();
  Graphics g = image.getGraphics();
  g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
  g.fillRect(0, 0, 600, 500);
  g.setColor(new Color(0,0,0));
  String number = String.valueOf(r.nextInt(99999));
  g.drawString(number, 5, 15);
  // 壓縮成jpeg格式
  OutputStream os = response.getOutputStream();
   //response.getWriter();//前面已經應用過response的輸出,主要是輸出文本具有編碼作用(字符流)
   //輸出普通字節流的話應該調用getOutputStream();
  //在這里也可以直接將其輸出,但是太大,可以通過JPEGCodec壓縮后輸出
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
  // 把BufferedImage對象中的圖像信息編碼后
  // 向創建該對象(encoder)時指定的輸出流輸出
  encoder.encode(image);
response.getWriter()--主要是輸出文本
response.getOutputStream()-----主要是輸出流
圖片識別技術----難度系數比較大
get是協議里面不能帶數據,但是可以通過地址欄來攜帶數據
get和post區別
IE瀏覽器,地址欄里面最多是2k
post提交的時候,其傳輸的數據受到服務器的限制
提交的不是文本的話,必須用post
練習中碰到的問題:
 a、out.println("<td><a href='del?id="+rs.getString(1)+"'>delete</a>"+"</td>")
 認真理解這么寫的格式:''與""的互相嵌套
 
 while(rs.next()){
    out.println("<tr>");
    out.println("<td>"+rs.getString(1)+"</td>");
    out.println("<td>"+rs.getString(2)+"</td>");
    out.println("<td>"+rs.getString(3)+"</td>");
    out.println("<td><a href='del?id="+rs.getString(1)+"'>delete</a>"+"<td>");
    out.println("</tr>");
   }
 在刷新的時候,同時將href重定向的地址動態加載
 在此練習中可以延伸下批量刪除如何實現(在這里可以變更成選擇框批量刪除);
 更改信息(ID不變)
 
 
 b、
 
 
 
 
10、如何用程序動態生成圖片?
 
11、為什么要配置隱藏input呢??
隱藏表單(隱藏域)
   <input type="hidden"   name="" value="">
     ====
     text
     checkbox
     button
     password
     sumbit
是為了提交form
12、Statement和PreparedStatement的參數索引是從1開始
  Class.forName("com.mysql.jdbc.Driver");
  Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","54255425");
  PreparedStatement ps=conn.prepareStatement("update emp set name=?,salary=? where id='"+id+"' ");
  
  ps.setString(1, name);//是給SQL語句中的第一個?賦予值
  ps.setFloat(2, Float.parseFloat(salary));//是給SQL語句中的第二個?賦值
  //ps.setString(parameterIndex, x),其中參數索引是從1開始
  //java.sql.SQLException: Parameter index out of range (3 > number of parameters,
  //which is 2).
 
 
 本文由用戶 honghu79 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!