सर्वर दो क्लाइंट - रास्पबेरी पीआई और एंड्रॉइड ऐप से अनुरोध प्राप्त करता है, दोनों HttpURLConnection का उपयोग करके अनुरोध भेजते हैं। मैं इन अनुरोधों के साथ पैरामीटर पास करने के, जैसे की जरूरत है:HttpServletRequest (Tomcat 9.0) से क्वेरी पैरामीटर कैसे प्राप्त करें?
http://192.168.0.10:8080/MyProject/MyServer/rpi/checktask?rpi="rpi"
के रूप में यह कर रही है:
String requestUrl = "http://192.168.0.10:8080/MyProject/MyServer/rpi";
String query = String.format("/checktask?rpi=%s",
URLEncoder.encode("rpi", "UTF-8"));
URL url = new URL(requestUrl + query);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.connect();
सर्वलेट है एनोटेशन:
@WebServlet(name = "MyServer", urlPatterns = { "/MyServer/rpi/*", "/MyServer/app/*"})
लेकिन जब सर्वलेट ऊपर निम्न होता है के रूप में अनुरोध हो जाता है :
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = request.getRequestURI(); // /MyProject/MyServer/rpi/*
String query = request.getQueryString(); // null
String context = request.getContextPath(); // /MyProject
String servlet = request.getServletPath(); // /MyServer/rpi
String info = request.getPathInfo(); // /*
}
हालांकि उन उत्तरों के अनुसार: How to use @WebServlet to accept arguments (in a RESTFul way)? और How come request.getPathInfo() in service method returns null?
यह इस तरह दिखना चाहिए:
String path = request.getRequestURI(); // /MyProject/MyServer/rpi//checktask?rpi="rpi"
String query = request.getQueryString(); // rpi="rpi"
String context = request.getContextPath(); // /MyProject
String servlet = request.getServletPath(); // /MyServer/rpi
String info = request.getPathInfo(); // /checktask?rpi="rpi"
क्या मैं गलत कर रहा हूँ?
यदि आप मैन्युअल रूप से यूआरएल मारने का प्रयास करते हैं (जैसे ब्राउज़र के साथ) यह काम करता है? – Taylor
यह ब्राउज़र और ऐप दोनों से काम नहीं करता है। – Katrikken
मेरा जवाब बकवास था, क्षमा करें। मैं अब थोड़ा उलझन में हूं और Servlet Spec को पढ़ रहा हूं, इस विषय पर सौवां समय .... :-( – gsl