0

I wrote the netbeans ta web service project. But when I write "50% discount" to the value of a parameter I use with http post, it looks like "P discount" to me. How can I fix this problem?

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

    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
emre kacan
  • 39
  • 3

1 Answers1

0

URLs are treated as encoded when received by the servlet. The % symbol followed by a 2 hex digits is the ASCII code of the character so %50 represents the letter P. To represent the % symbol you have to send %25 to represent the % symbol.

Your URL should be:

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

Here you can find a list of the character codes:

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

whbogado
  • 929
  • 5
  • 15