data in web applications
DESCRIPTION
Data in Web Applications. XML, Uploading, Importing... Joshua Scotton. Forcing No-Cache in JSP.TRANSCRIPT
![Page 1: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/1.jpg)
Data in Web Applications
XML, Uploading, Importing...Joshua Scotton
![Page 2: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/2.jpg)
<% //HTTP 1.1 response.setHeader("Cache-Control","no-store"); //HTTP 1.0 response.setHeader("Pragma","no-cache"); //Stops any problems from proxy servers response.setDateHeader ("Expires", 0); %>
Forcing No-Cache in JSP
![Page 3: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/3.jpg)
Simplest way is to construct a string:String s = “<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n” + “<exampleDoc>\n” + “ <xml-tag>\n” + “ ...\n” + “ </xml-tag>\n” + “</exampleDoc>”;
Generating XML
![Page 4: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/4.jpg)
Have to handle special characters◦ <tag><br/></tag>
No validation so easy to make mistakes
Disadvantages
![Page 5: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/5.jpg)
Java API for XML Processing Provides validation, parsing and generation
functionality for XML
JAXP
![Page 6: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/6.jpg)
DOM - Document Object Model (JDK5+) SAX - Simple API for XML (JDK5+) StAX - Streaming API for XML (JDK6+)
Java Built-In XML Parsers
![Page 7: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/7.jpg)
The DOM Parser models the entire XML document in memory
Uses the org.w3c.dom.Document model Tree Nodes
◦ Element◦ Attr◦ Text
DOM XML Parser
![Page 8: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/8.jpg)
Generate XML with DOM
G e n e r a t e D O M . c l a s s
![Page 9: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/9.jpg)
Parse XML with DOMR e a d D O M . c l a s s
![Page 10: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/10.jpg)
Modify XML with DOMM o d i f y D O M . c l a s s
![Page 11: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/11.jpg)
DOM should be avoided for large files as the entire XML file is modelled as a Document in memory, including all Nodes, Elements, Attributes etc.
Disadvantage of DOM
![Page 12: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/12.jpg)
Faster at reading XML content than DOM. Stream implementation instead of
Document Model Callback handler object has to be created
SAX XML Parser
![Page 13: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/13.jpg)
Following methods can be overwritten in the Handler class:◦ startDocument() and endDocument() –
methods called at the start and end of an XML document.
◦ startElement() and endElement() – methods called at the start and end of a document element.
◦ characters() – method called with the text contents in between the start and end tags of an XML document element.
SAX Callback Handler Methods
![Page 14: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/14.jpg)
Parse XML with SAXR e a d S A X . c l a s s
![Page 15: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/15.jpg)
QuizMaster
![Page 16: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/16.jpg)
Upload and import XML test files Store tests, results and users in database User login and registration Users can take tests and view results
Functionality
![Page 17: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/17.jpg)
Test◦ TestQuestion
TestAnswer Result
◦ ResultQuestion
Quiz Representation
![Page 18: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/18.jpg)
<!ELEMENT answer ( text ) > <!ATTLIST answer id NMTOKEN #REQUIRED > <!ATTLIST answer score NMTOKEN #REQUIRED > <!ELEMENT description ( #PCDATA ) > <!ELEMENT question ( text, answer+ ) > <!ATTLIST question id NMTOKEN #REQUIRED > <!ELEMENT test ( title, description, question+ ) > <!ELEMENT text ( #PCDATA ) > <!ELEMENT title ( #PCDATA ) >
XML DTD
![Page 19: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/19.jpg)
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE root_element PUBLIC "Test"
"http://localhost:8080/QuizMaster/test.dtd"><test> <title>Example Quiz</title> <description>Quick Quiz on Java XML Parsers</description> <question id="1"> <text>Which of the following is not a built-in Java parser?</text> <answer score="1" id="1“><text>JDOM</text></answer> <answer score="0" id="2“><text>DOM</text></answer> <answer score="0" id="3“><text>SAX</text></answer> </question> <question id="2"> <text>DOM loads the entire XML into memory. True or false?</text> <answer score="1" id="1“><text>True</text></answer> <answer score="0" id="2“><text>False</text></answer> </question></test>
XML Example
![Page 20: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/20.jpg)
public class TestBean implements Serializable {
private String title;private String description;private List<TestQuestionBean> questions;
public String getTitle() { return title; }public void setTitle(String title) { this.title = title; }public String getDescription() { return description; }public void setDescription(String description) {this.description = description; }
public List<TestQuestionBean> getQuestions() { return questions; }
public void setQuestions(List<TestQuestionBean> questions) {this.questions = questions; }
public void addQuestion(TestQuestionBean question) {this.questions.add(question); }
}
TestBean
![Page 21: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/21.jpg)
public class TestQuestionBean implements Serializable {
private String text;private String id;private List<TestAnswerBean> answers;
public String getText() { return text; }public void setText(String text) { this.text = text; }
public List<TestAnswerBean> getAnswers() { return answers; }
public void setAnswers(List<TestAnswerBean> answers) {this.answers = answers; }
public void addAnswer(TestAnswerBean answer) { this.answers.add(answer); }
}
TestQuestionBean
![Page 22: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/22.jpg)
public class TestAnswerBean implements Serializable {private String text;private String id;private Integer score;
public String getText() {return text;
}public void setText(String text) {this.text = text;
}public Integer getScore() {return score;
}public void setScore(Integer score) {this.score = score;
}}
TestAnswerBean
![Page 23: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/23.jpg)
XML -> JavaBeansI m p o r t T e s t . c l a s s
![Page 24: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/24.jpg)
Select file (uploadForm.jsp) Load into UploadBean (importUpload.jsp) UploadBean -> ImportTest ImportTest creates TestBean TestBean stored in Session Success Message (importUpload.jsp) Display Test (viewTest.jsp)
index?action=“upload|import|view”
File Upload
![Page 25: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/25.jpg)
<form enctype="multipart/form-data" method="POST" action=“admin?action=import">
File Path:<br /><input type="file" id="filePath" name="filePath" /><br /><br />
<input type="submit" value="Upload" /></form>
uploadForm.jsp
![Page 26: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/26.jpg)
<jsp:useBean id="uploader" class="webdev.quizmaster.UploadBean" scope="session"/>
<p><%= uploader.startUpload(request) %></p><p><a href="index?action=view">View Test</a></p>
importUpload.jsp
![Page 27: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/27.jpg)
Catching the UploadU p l o a d B e a n . c l a s s
![Page 28: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/28.jpg)
<jsp:useBean id="test" class="webdev.quizmaster.TestBean" scope="session"/>
<h3><%=test.getTitle() %></h3><p><i><%=test.getDescription() %></i></p>
<% int i=0;for(TestQuestionBean q : test.getQuestions()){
i++;out.println("<hr width=\"95%\"><p>Question " + i + ": " + q.getText()+"</p><ul>");
for(TestAnswerBean a : q.getAnswers()){out.println("<li><i>" + a.getText() + "</i></li>");}
out.println("</ul>");}%>
viewTest.jsp
![Page 29: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/29.jpg)
Adding Authentication
![Page 30: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/30.jpg)
connectionName connectionPassword connectionURL driverName roleNameCol userCredCol userNameCol userRoleTable userTable
org.apache.catalina.realm.JDBCRealm
![Page 31: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/31.jpg)
CREATE TABLE `QuizMaster`.`user` (`username` TEXT NOT NULL ,`password` TEXT NOT NULL
) ENGINE = InnoDB;
CREATE TABLE `QuizMaster`.`role` (`username` TEXT NOT NULL ,`role` TEXT NOT NULL
) ENGINE = InnoDB;
SQL for Tables
![Page 32: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/32.jpg)
Will need to install the MySQL driver from http://dev.mysql.com/downloads/connector/j/3.0.html in the tomcat/lib folder
<Realm className="org.apache.catalina.realm.JDBCRealm"driverName="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/quizmaster"connectionName=“username"connectionPassword=“password"userTable="user"userNameCol="username"userCredCol="password"userRoleTable="role"roleNameCol="role"/>
JDBCRealm in Context.xml
![Page 33: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/33.jpg)
admin:admin guest:guest
Setup Users
![Page 34: Data in Web Applications](https://reader036.vdocument.in/reader036/viewer/2022062411/56816722550346895ddba823/html5/thumbnails/34.jpg)
admin User
Setup Roles