Prediction class (javabean)
Package predictions;
Import java. io. Serializable;
/**
* Created by AlexY on 2016/6/28.
*/
Public class Prediction implements Serializable {
Private static final long serialVersionUID = 757008977188042582l;
Private String who; // person
Private String what; // his prediction
Public Prediction (){
}
Public String getWhat (){
Return what;
}
Public void setWhat (String what ){
This. what = what;
}
Public String getWho (){
Return who;
}
Public void setWho (String who ){
This. who = who;
}
}
Predictions class (javabean)
Read text content from the predictions. db file to the predictions array, and serialize the array to xml
Package predictions;
Import javax. servlet. ServletContext;
Import java. beans. XMLEncoder;
Import java. io .*;
/**
* Created by AlexY on 2016/6/28.
*/
Public class Predictions {
Private int n = 32;
Private Prediction [] predictions;
Private ServletContext servletContext;
Public Predictions (){
}
// Servlet is used to read data from the text file in the war file
Public ServletContext getServletContext (){
Return servletContext;
}
Public void setServletContext (ServletContext sctx ){
This. servletContext = sctx;
}
// Empty implement
Public void setPredictions (Prediction [] ps ){}
// GetPredictions returns an xml file that represents the Predictions array
Public String getPredictions (){
// Check whether ServletContext is set
If (getServletContext () = null ){
Return null;
}
// Check whether data has been read
If (predictions = null ){
Populate ();
}
// Convert the Predictions array to an xml document
Return toXML ();
}
// Convert the Predictions array to an xml document
Private String toXML (){
String xml = null;
ByteArrayOutputStream out = new ByteArrayOutputStream ();
XMLEncoder encoder = new XMLEncoder (out );
Encoder. writeObject (predictions); // serialize to xml
Encoder. close ();
Xml = out. toString ();
Return xml;
}
// Read the content in the text to the predictions array
Private void populate (){
String filename = "/WEB-INF/data/predictions. db ";
InputStream in = servletContext. getResourceAsStream (filename );
// Read the content of the text file to the predictions array
If (null! = In ){
// Because each record in the file is a row, and the who and what fields of each row are passed "! "Separated
InputStreamReader isr = new InputStreamReader (in );
BufferedReader reader = new BufferedReader (isr );
String line = null;
Int I = 0;
Try {
Predictions = new Prediction [n];
While (null! = (Line = reader. readLine ())){
String [] parts = line. split ("! ");
Prediction p = new Prediction ();
P. setWho (parts [0]);
P. setWhat (parts [1]);
Predictions [I ++] = p;
}
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
}
Predictions. db:
The read text file records one line and each line contains "! "Is separated into two parts.
Cornelius Tillman! Managed holistic contingency will grow killer action-items.
Conner Kulas! Vision-oriented zero administration time-frame will generate back-end interfaces.
Loraine Ryan! Triple-buffered scalable function will productize visionary infomediaries.
Patricia Zulauf! Reactive radical knowledge base will aggregate extensible vortals.
River Wiza! Face to face client-server pricing structure will whiteboard robust communities.
Jarred Wehner! Future-proofed 5th generation protocol will strategize web-enabled networks.
Emily Roob! Cross-group fresh-thinking encoding will reinvent dot-com systems.
Elvis Ernser! Customizable assymetric database will be visualize virtual action-items.
Kathryn Hilpert! User-centric non-volatile open architecture will iterate world-class vortals.
Tanner Dietrich! Enhanced zero tolerance system engine will evolve turn-key deliverables.
Linnie Funk! Distributed dynamic moratorium will iterate magnetic e-commerce.
Emery Ward! Synergistic demand-driven functionalities will be visualize compelling vortals.
Craig Leuschke! Robust intermediate exists will facilitate best-of-breed synergies.
Shayna Lehner! Digitized optimal conglomeration will exploit proactive relationships.
Holls McCullough! Universal fault-tolerant architecture will synthesize bleeding-edge channels.
Mina Hayes! Cloned assymetric intranet will enable innovative functionalities.
River Friesen! Decentralized 24/7 hub will target robust web-readiness.
Carmel Becker! Synergistic disintermediate policy will expedite back-end experiences.
Bartholome Walsh! Triple-buffered didactic emulation will visualize integrated channels.
Russel Robel! Retriable intangvisible alliance will scale sexy ROI.
Charlene Mertz! Triple-buffered neutral collaboration will incubate B2B deliverables.
Letitia Pacocha! User-centric multi-state success will transform proactive convergence.
Lottie Marks! Open-source multi-tasking time-frame will monetize rich partnerships.
Kaden Crona! Optional static definition will unleash dynamic e-tailers.
Everardo Lind! De-engineered systematic emulation will deploy out-of-the-box partnerships.
Lilyan Thompson! Synergistic 24/7 website will transition 24/7 methodologies.
Alessia O' Connell! Reactive value-added middleware will engineer next-generation partnerships.
Reymundo Champlin! Self-enabling reciprocal synergy will generate seamless portals.
Immanuel Bergstrom! Assimilated intermediate superstructure will drive vertical methodologies.
Dahlia Robel! Proactive demand-driven open architecture will innovate impactful networks.
Deven Blanda! Balanced clear-thinking utilisation will expedite collaborative initiatives.
Hiram gulgoski! Versatile tangible application will maximize rich e-business.
Predictions. jsp:
The generated xml document is displayed. The html body and other content are removed here.
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" %>
<% @ Page import = "predictions. Prediction" %>
<Jsp: useBean id = "preds" class = "predictions. Predictions"> </jsp: useBean>
<%
String verb = request. getMethod ();
// Determine the http request type of the client
If (! Verb. Inclusignorecase ("GET ")){
Response. sendError (response. SC _METHOD_NOT_ALLOWED, "only GET requests allowed ");
} Else {
// Set ervletContext of prediction
Preds. setServletContext (application );
// Output xml to the webpage
Out. print (preds. getPredictions ());
}
%>
Test: curl access:
$ Curl -- request GET http: // localhost: 8080/predictions. jsp