0

I'm quite used to PHP and have just started working with Java server side scripting. I've been told that it's bad practice to use scriptlets but I've used them anyway for testing purposes.

<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%@page import="java.net.*"%>
<%
    String url = "https://www.google.com/recaptcha/api/siteverify";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", "USER_AGENT");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    String urlParameters = "secret=6LeI6AgTAAAAAGJDs5rvWB67-d7n5YjN61JW1RKn&response="
+request.getParameter("g-recaptcha-response");

    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    out.println("\nSending 'POST' request to URL : " + url);
    out.println("Post parameters : " + urlParameters);
    out.println("Response Code : " + responseCode);
        BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer responses = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        responses.append(inputLine);
    }
    in.close();
    out.println(responses.toString());  
%>

Here the content is not really relevant to the question.I needed a way to parse the json response which is quite simple using json_decode() in PHP .
I have downloaded JSONObject.java, JSONString.java ... and all the java files given at http://www.json.org/java/index.html .

Is there any way I could include these java files so I could use the classes available in these java files?
I assumed it would be along the lines of :

<%@page import="JSONArray.*"%>
Whoophee
  • 3
  • 5
  • 1
    may be this will help [how to import classes in jsp]: http://stackoverflow.com/questions/239147/how-do-you-import-classes-in-jsp – Swap905 Jun 30 '15 at 09:26
  • I've gone through that before. The library he has mentioned is implemented by default by tomcat. However I can't figure how to include the downloaded libraries. – Whoophee Jun 30 '15 at 09:46

1 Answers1

0

You cannot import .java files, you have to import (compiled) classes. Download compiled .class files, or better, a .jar file with the whole library (because the separate classes most probably won't work) and place it into WEB-INF/lib folder of your web application. Then you can import the class(es) from the library using the above mentioned page import directive, e.g.

<%@page import="org.json.JSONArray" %>

You have to specify the fully qualified name of the class, i.e. org.json.JSONArray, just JSONArray is not enough. And you will probably need to restart (reload) your web application, or even the (Java) server.

If you are going to tackle with Java/JSP more, I would recommend you to study some basics. Start with learning what the classpath is, then you can continue here.

Community
  • 1
  • 1
Jozef Chocholacek
  • 2,874
  • 2
  • 20
  • 25