Netbeans Http Post Character

时间:2017-04-26 18:34:00

标签: java http servlets

我写了netbeans ta web服务项目。但是当我对http post使用的参数值写“50%折扣”时,对我来说它看起来像“P discount”。我该如何解决这个问题?

  

192.168.0.222:7001/Project/KonuEkle?uye=test&&baslik=%50 discount&& mesaj = test&& kategori = 123&& link = null

import com.mrkcn.servlet.Classlar.ConnectInfo;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;

public class KonuEkleServlet extends HttpServlet {
    public String kullaniciadi;
        public String baslik;
        public String mesaj;
        public String kategori;
        public String altKategori;
        public String link;
        public Connection con;
         boolean action = false;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            try {
                req.setCharacterEncoding("UTF-8");
                resp.setCharacterEncoding("UTF-8");

                ConnectInfo conServlet= new ConnectInfo();
                con=null;
                con=conServlet.baglanti();

                PreparedStatement pstmt=null;
                ResultSet rs=null;
                Boolean konuEkleKontrol = false;

                PrintWriter out = resp.getWriter();
                JSONObject j = new JSONObject();
                ArrayList<String> konuEkleList = new ArrayList<String>(100);

                kullaniciadi = req.getParameter("uye");
                baslik = req.getParameter("baslik");
                mesaj = req.getParameter("mesaj");
                kategori = req.getParameter("kategori");
                link = req.getParameter("link");

                j.put("mesaj1",baslik);

                //altKategori = req.getParameter("altkategori");
                //kategoriBilgiGetir(kategori , altKategori);

                String konuEkle="insert into konular(uye,baslik,mesaj,kategori,tarih,edittarih,aktif,indirimpuani,altkategori,link) values (?,?,?,?,GETDATE(),NULL,1,0,0,?)";
                pstmt=con.prepareStatement(konuEkle);
                pstmt.setString(1, kullaniciadi);
                pstmt.setString(2, baslik);
                pstmt.setString(3, mesaj);
                pstmt.setString(4, kategori);
                pstmt.setString(5, link);

                   int count = pstmt.executeUpdate();
                   action = (count > 0); 
                   if (action)
                   {
                       j.put("mesaj","basarili");
                       konuEkleList.add(j.toString());
                       out.write(konuEkleList.toString());
                       out.close();
                   }
                   else
                   {
                   j.put("mesaj","basarisiz");
                       konuEkleList.add(j.toString());
                       out.write(konuEkleList.toString());
                       out.close();
                   }


            } catch (SQLException ex) {
                Logger.getLogger(KonuEkleServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (JSONException ex) {
                Logger.getLogger(KonuEkleServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
                        }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }
}

1 个答案:

答案 0 :(得分:0)

当servlet收到URL时,URL被视为已编码。 %符号后跟2个十六进制数字是字符的ASCII码,因此%50表示字母P.要表示%符号,您必须发送%25来表示%符号。

您的网址应为:

192.168.0.222:7001/Project/KonuEkle?uye=test&&baslik=%2550 discount&&mesaj=test&&kategori=123&&link=null

您可以在此处找到字符代码列表:

https://www.w3schools.com/tags/ref_urlencode.asp