1

I'm building a servlet for a school assignment, the assignment is to build an access point for a airplane with a captive portal (servlet)

The structure is build but the connection with the database is a big question mark.

We have two pages:

  1. the index page/landing page
  2. the rerouting page

On page one there is a text field where the user has to fill in his or her ticketnumber.

Under here page 1.

PrintWriter out = response.getWriter();


        out.println("<!doctype html>");
        out.println("<html lang=\"en-US\">");
        out.println("<head>");

            out.println("<meta charset=\"utf-8\">");

            out.println("<title>Login</title>");

            out.println("<link rel=\"stylesheet\" href=\"css/style.css\">");
        out.println("</head>");

        out.println("<body background=\"images/bg.jpg\">");

            out.println("<section>");
                out.println("<div id=\"login\">");

                out.println("<img class=\"headerImage\" src=\"images/corendon.png\"/>");
                out.println("<img class=\"ticketIcon\" src=\"images/ticketIcon.png\"/>");

                    out.println("<h2></h2>");

                    out.println("<form name=\"myForm\" action=\"loginPage\" method=\"get\" onSubmit=\"event.preventDefault(); redirect(); return false;\">");

                        out.println("<fieldset>");

                            out.println("<p><label for=\"upper\"></label></p>");

                            out.println("<div>");
                                out.println("<p><input id=\"ticketnumber\" placeholder=\"ticketnumber..\" name=\"ticketnummer\"/></p>");
                            out.println("</div>");

                            out.println("<input type=\"checkbox\" id=\"cb1\" name=\"cb1\"/>");
                            out.println("<label for=\"cb1\" id=\"cb1text\">Ik accepteer de voorwaarden</label>");

                            out.println("<p><input type=\"submit\" value=\"Go Online\"></p>");

                            out.println("<p><span id=\"validation\"></span></p>");
                            out.println("</fieldset>");

                    out.println("</form>");
                out.println("</div>");
            out.println("</section>");

                out.println("<script type=text/javascript>");
                    out.println("function redirect(){");
                        out.println("var x = document.myForm.ticketnummer.value;");
                        out.println("var ticketnummer = \"1234\";");
                        out.println("if ((cb1.checked == 1)&&(x == ticketnummer)){");
                            out.println("window.location=\"loginPage?ticketnummer=\" + x");
                        out.println("}else {");
                            out.println("document.getElementById('validation').innerHTML=\"login onsuccesvol\";");
                            out.println("return false;");
                            out.println("}");

                    out.println("}");
                out.println("</script>");

        out.println("</body>");
        out.println("</html>");

onder here page 2:

PrintWriter out = response.getWriter();

    String ticketNumber = request.getParameter("ticketnummer");

    // ip address
    String ipAddress = request.getHeader("X-FORWARDED-FOR");  
        if (ipAddress == null)
        {  
            ipAddress = request.getRemoteAddr();  
        };


        out.println("<!doctype html>");
        out.println("<html lang=\"en-US\">");
        out.println("<head>");

            out.println("<meta charset=\"utf-8\">");

            out.println("<title>LoginAccepted</title>");

            out.println("<link rel=\"stylesheet\" href=\"css/styleLoginPage.css\">");
        out.println("</head>");

        out.println("<body background=\"images/bg.jpg\">");

            out.println("<section>");
                out.println("<div id=\"login\">");


                out.println("<img class=\"headerImage\" src=\"images/corendon.png\"/>");


                    out.println("<h2></h2>");

                    out.println("<form>");

                        out.println("<fieldset>");

                            out.println("<p><label for=\"upper\"></label></p>");
                                out.println("<div id=\"welcome\">");

                                        out.println("U bent nu ingelogd in het netwerk!");
                                        out.println("Corendon wenst u een fijne vlucht en");
                                        out.println("veel internetplezier toe.");
                                        out.println("<img class=\"loginImage\" src=\"images/loginImage.png\"/>");

                                out.println("</div>");
                        out.println("</fieldset>");

                    out.println("</form>");
                out.println("</div> <!-- end login -->");
            out.println("</section>");

        out.println("</body>");
        out.println("</html>");

        if (ticketNumber.equals("1234")) {
            try {  
                 Runtime rt = Runtime.getRuntime ();

                 Process process = rt.exec ("/var/apache-tomcat-8.0.23/unblock.sh " + ipAddress);

                 InputStreamReader reader = 
                 new InputStreamReader ( process.getInputStream () );  

                 BufferedReader buf_reader =  
                 new BufferedReader ( reader ); 

                 String line;  
                 while ((line = buf_reader.readLine ()) != null)  
                 System.out.println (line);  

                 }  
                    catch (IOException e) {  
                        System.out.println (e);
                 }
        }

my question: how do I add the json query in these scripts?

for example:

{
"function":"List",
"teamId": "IN[key]",
"teamKey":"[key]",
"requestId": "0"
}

the feedback will be:

{"requestId":"0", "result":"0", "passengers":[{"uni":"d565c05f-596b-46a2-b302-a055148b8fb1","surName":"Boogaard","firstName":"Chiel"},{"uni":"e431fc48-c2bf-4e84-a7c6-414e5b86dff9","surName":"","firstName":""},{"uni":"1c7e3cee-b445-45ab-9397-cdc7c80370c8","surName":"Wolf","firstName":"Jesse"},{"uni":"22ad0d5c-698e-4711-9b91-e21bf9d6e932","surName":"Hansali","firstName":"Amin"}]}

and the ticket number request:

{
"function":"List",
"teamId": "IN103-1",
"teamKey": "c2f590a4-abab-4e73-b65f-1aef785c6c75",
"requestId" : "0"
}

and feedback:

{"requestId":"0", "result":"0", "tickets":[{"uni":"e092058c-da83-4970-829a-a56a516fd6a3","usedInternet":"0","uniPassenger":"e431fc48-c2bf-4e84-a7c6-414e5b86dff9","uniFlight":"719d922c-e38d-4aac-afe2-4a36ba900865","ticketNumber":"1234567890"}]}

how do I check the filled in ticketnumber in the servlet with the database using json? by the way I cannot use PHP or ASP

5
  • so you want to receive parameters as json and write some output as json to the response instead of html? Commented Dec 14, 2015 at 11:48
  • yes i know its not the right way, but after the project i will never use the servlet or maintain it Commented Dec 14, 2015 at 14:19
  • i am trying to make: if ((cb1.checked == 1)&&(x == ticketnummer)){ and the ticket number has to be checked at the database Commented Dec 14, 2015 at 14:19
  • @chiel.boogaard, but someone else will have to, somewhere, someday :`( legacy projects... Commented Dec 14, 2015 at 14:29
  • i know but its for this one project at school and thats it, it will be tested and will be showed to my teacher and then press delete :P Commented Dec 14, 2015 at 21:44

1 Answer 1

2

You did mess with your project. Please follow: 1. Create jsp file in the web folder - there are your request and response params. Freely use html here. If there are problems include jsp-api.jar into the project.

  1. Use JQuery in your front end project. Download jquery from anywhere there are tons of copies on the internet - download only one of them. Include jquery.js file in above mentioned jsp.

  2. Open a script tag and then write:

function queryData(your_query_object){

$.ajax({

   url: 'some_servlet',

   type: 'GET',

   data: your_query_object,

   success: function(response){

                          alert(response);

                      },

   error: function(reason){

                alert(reason);
              }

});

}

Then call that function with your query object:

  queryData(queryObject);

By calling this function you are sending http request to some servlet. And handle the request just as you do it in usual cases - parameter names are just as you put in your json query object. Your query object is

{ "function":"List", "teamId": "IN103-1", "teamKey": "c2f590a4-abab-4e73-b65f-1aef785c6c75", "requestId" : "0" }

So get these values in the servlet like this:

request.getParameter("function");

I am supposing you know javascript, you know also http rules; and I hope you have learned how servlet-mapping is done. If NO you should go and read something about webapp with java and javascript.

Also consider no one makes your homework for you.

Sign up to request clarification or add additional context in comments.

4 Comments

haha yes i know no one makes my homework :P but i cant figure this part out so it turned to the community to help me out. and thank you! this helped me alot already!
At the beginning I also asked any questions that I was stuck with. Then I got downvoting. So I try to not to ask question, which could have probability at least 30% of existance.
sorry i dont fully understand your comment
In your question you asked too much things which does not suite to Stackoverflow standards. Question must be oriented to a specific problem, and form of answer is not excepted as well. Your question is to broad...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.