1 / 34

Data in Web Applications

Data in Web Applications. XML, Uploading, Importing... Joshua Scotton. Forcing No-Cache in JSP. <% // HTTP 1.1 response.setHeader ("Cache- Control","no -store"); // HTTP 1.0 response.setHeader (" Pragma","no -cache"); // Stops any problems from proxy servers

miyo
Download Presentation

Data in Web Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data in Web Applications XML, Uploading, Importing... Joshua Scotton

  2. Forcing No-Cache in JSP <% //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); %>

  3. Generating XML • 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>”;

  4. Disadvantages • Have to handle special characters • <tag>&lt;br/&gt;</tag> • No validation so easy to make mistakes

  5. JAXP • Java API for XML Processing • Provides validation, parsing and generation functionality for XML

  6. Java Built-In XML Parsers • DOM - Document Object Model (JDK5+) • SAX - Simple API for XML (JDK5+) • StAX - Streaming API for XML (JDK6+)

  7. DOM XML Parser • The DOM Parser models the entire XML document in memory • Uses the org.w3c.dom.Document model • Tree Nodes • Element • Attr • Text

  8. Generate XML with DOM GenerateDOM.class

  9. Parse XML with DOM ReadDOM.class

  10. Modify XML with DOM ModifyDOM.class

  11. Disadvantage of DOM • 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.

  12. SAX XML Parser • Faster at reading XML content than DOM. • Stream implementation instead of Document Model • Callback handler object has to be created

  13. SAXCallback Handler Methods • 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.

  14. Parse XML with SAX ReadSAX.class

  15. QuizMaster

  16. Functionality • Upload and import XML test files • Store tests, results and users in database • User login and registration • Users can take tests and view results

  17. Quiz Representation • Test • TestQuestion • TestAnswer • Result • ResultQuestion

  18. XML DTD <!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 ) >

  19. XML Example <?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>

  20. TestBean 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); } }

  21. TestQuestionBean 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); } }

  22. TestAnswerBean 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; } }

  23. XML -> JavaBeans ImportTest.class

  24. File Upload • 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”

  25. uploadForm.jsp <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>

  26. importUpload.jsp <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>

  27. Catching the Upload UploadBean.class

  28. viewTest.jsp <jsp:useBean id="test" class="webdev.quizmaster.TestBean" scope="session"/> <h3><%=test.getTitle() %></h3> <p><i><%=test.getDescription() %></i></p> <% inti=0; for(TestQuestionBeanq : test.getQuestions()){ i++; out.println("<hr width=\"95%\"><p>Question " + i + ": " + q.getText()+"</p><ul>"); for(TestAnswerBeana : q.getAnswers()){ out.println("<li><i>" + a.getText() + "</i></li>"); } out.println("</ul>"); } %>

  29. Adding Authentication

  30. org.apache.catalina.realm.JDBCRealm • connectionName • connectionPassword • connectionURL • driverName • roleNameCol • userCredCol • userNameCol • userRoleTable • userTable

  31. SQL for Tables 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;

  32. JDBCRealm in Context.xml • Will need to install the MySQLdriver 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" />

  33. Setup Users • admin:admin • guest:guest

  34. Setup Roles • admin • User

More Related