我正在为学校作业构建一个servlet,其任务是为拥有强制网络门户(servlet)的飞机构建一个接入点
结构是构建的,但与数据库的连接是一个很大的问号。
我们有两页:
在第一页上有一个文本字段,用户必须填写他或她的票号。
在这里第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);
}
}
我的问题:如何在这些脚本中添加json查询?
例如:
{
"function":"List",
"teamId": "IN[key]",
"teamKey":"[key]",
"requestId": "0"
}
反馈将是:
{"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"}]}
和票号请求:
{
"function":"List",
"teamId": "IN103-1",
"teamKey": "c2f590a4-abab-4e73-b65f-1aef785c6c75",
"requestId" : "0"
}
和反馈:
{"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"}]}
如何使用json检查servlet中填充的ticketnumber? 顺便说一句,我不能使用PHP或ASP
答案 0 :(得分:2)
在前端项目中使用JQuery。从互联网上有大量拷贝的任何地方下载jquery - 只下载其中一个。 在上面提到的jsp中包含jquery.js文件。
打开脚本标记,然后写:
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);
}
});
}
然后用您的查询对象调用该函数:
queryData(queryObject);
通过调用此函数,您将向某个servlet发送http请求。并像处理通常情况一样处理请求 - 参数名称就像放在json查询对象中一样。您的查询对象是
{ “功能”:“名单”, “teamId”:“IN103-1”, “teamKey”:“c2f590a4-abab-4e73-b65f-1aef785c6c75”, “requestId”:“0” }
所以在servlet中获取这些值如下:
的request.getParameter( “功能”);
我想你知道javascript,你也知道http规则;我希望你已经学会了如何完成servlet映射。如果不是,你应该去阅读有关使用java和javascript的webapp的内容。
还要考虑没有人为你做作业。