2I1073 Lektion 2 KTH-MI Peter Mozelius Servlets, säkerhet, och filhantering.

Slides:



Advertisements
Similar presentations
Fondamenti di Java Introduzione alla costruzione di GUI (graphic user interface)
Advertisements

J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
Layouts and Graphics. component - container - layout Un Container contiene [0 o +] Components Il Layout specifica come i Components sono disposti nel.
1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
FH-Hof Dialoge Richard Göbel. FH-Hof Inhalt JDialog Vordefinierte Dialoge File Browser.
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Chapter 16 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.
Taking Input Java Md. Eftakhairul Islam
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
JSP and Servelets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
Introduction to Swing Components Chapter 14. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
ITK:P1 Lektion 6 Den abstrakta datatypen kö DSV Peter Mozelius.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
Graphical User Interfaces
JAVA1 1. JFrame. JAVA2 JApplet import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HerhalingAppletH10 extends JApplet implements.
ตัวอย่างการพัฒนา ระบบงาน. 2 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SignInWindow extends JFrame { JPanel panel;
Import javax.swing.*; public class FrameTest { public static void main(String args[]) { JFrame f = new JFrame("Frame Test"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLO.
JLab Lattice Portal – Data Grid Web Service Ying Chen, Chip Watson Thomas Jefferson National Accelerator Facility.
1 cs205: engineering software university of virginia fall 2006 Wimpy Interfaces.
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Web Design & Development Lecture 19. Java Graphics 2.
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Exercises of the Tutorial on Advanced Web Programming Authors: Miroslava Mitrovic Dragan Milicev Nino.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Advance Java Technology Java’s Role::MVC Architecture :: Swing Prof.Chintan Dave.
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components.
Cookie in a servlet. Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when visiting.
Page w16.1 – Spring 2010Steffen Vissing Andersen SDJ I1 subjects, Spring 2010 Agenda – Week 16, 2010 GUI.
1 MSPEL Lektion 3 DSV Peter Mozelius Grafik och spel i Java applets.
ITK:P1 Lektion 2 Klassarv, polymorfism och grafiska komponenter DSV Peter Mozelius.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Graphical User Interfaces Java’s AWT and Swing APIs.
Graphical User Interfaces (Part IV)
Java Software Development Paradigm Lecture # 12. Basics of GUI.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
Graohics CSC 171 FALL 2001 LECTURE 16. History: COBOL Conference on Data System Languages (CODASYL) - led by Joe Wegstein of NBS developed the.
Drawing in a frame – Java GUI
CS18000: Problem Solving and Object-Oriented Programming.
The Model-View Approach in Java. OK, you’ve got “Hello World” running... What now?
F27SB2 Programming Languages
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Apache Tomcat as a container for Servlets and JSP
Java Server Pages (JSP)
Java Servlets Java Server Pages (JSP)
Swing: the art of the GUI COMP204, Bernhard Pfahringer.
JAVA 程式語言入門 (II).  版面配置  事件驅動  Ch14_01.java 1. import javax.swing.*; 2. import java.awt.*; 3. class Ch14_01 4. { 5. public static void main(String.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Java Applets- Using SwingWorker Dave Price and Chris Loftus Computer Science Department University of Wales, Aberystwyth.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Övning 5. Repetition klasser class Rektangel { private static int antal = 0; private double längd; private double bredd; public Rektangel(double l, double.
GUI and Event-Driven Programming Recitation – 3/6/2009 CS 180 Department of Computer Science, Purdue University.
Layout Mangers CSC 171 FALL 2001 LECTURE 14. History: The Transistor William Shockley, John Bardeen, and Walter Brattain invent the transfer resistance.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
Graphical User Interface
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

2I1073 Lektion 2 KTH-MI Peter Mozelius Servlets, säkerhet, och filhantering

Lektion 2a import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Lektion2a extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

Lektion 2a res.setContentType("text/html"); PrintWriter out = res.getWriter(); String protokoll = req.getProtocol(); // Skriv ut som ett validerande XHTML1.1-dokument out.println( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\""); out.println( "\" out.println( " ");

Lektion 2a out.println(" "); out.println( " <meta http-equiv=\"Content-Type\" content=\"text/html;" + "charset=iso \" />"); out.println(" 2I1073-Lektion2a " ); out.println(" " ); out.println(" Hej, detta är en hälsning via " + protokoll + " " ); out.println(" " );

Lektion 2a out.println(" " ); out.close(); }//doGet public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doGet(req, res); }//doPost }//Lektion2a

Lektion 2b - web.xml - Lektion2a Lektion2a

Lektion 2b - web.xml - Lektion2a /Lektion2a 15 min paus!

Lektion 2c import java.io.*; import java.security.*; public class Lektion2c { public static void main(String[] args) { String recept = ""; //öppna filen med recept och läs in BufferedReader receptFil = null;

Lektion 2c try { receptFil = new BufferedReader( new FileReader("recept.fil")); String rad; while((rad = receptFil.readLine()) != null) recept += "\n" + rad; } catch (FileNotFoundException fnfe){ System.err.println("Filen hittades inte!"); System.exit(1); } catch (IOException e){ System.err.println("Filen gick inte att läsa!"); System.exit(2); }

Lektion 2c //låt klassen MessageDigest beräkna ett kontrollvärde byte[] buffer = recept.getBytes(); try { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(buffer); System.out.println(md.digest()); System.out.print("är kontrollvärdet för följande recept: \n\n"); System.out.println(recept); } catch(NoSuchAlgorithmException nsae) { System.err.println(nsae.getMessage()); } }//main

Lektion 2d import java.io.*; import javax.swing.*; import javax.swing.filechooser.*; import java.awt.*; import java.awt.event.*; public class Lektion2d extends JFrame { private Container container; private JTextField infoRuta; private JTextArea meddelandeRuta; private JPanel nordPanel, mittPanel, sydPanel;

Lektion 2d private JMenuBar filBar; private JMenu arkivMeny; private JMenuItem mItem; private String filnamn; public Lektion2d() { skapaGränsSnitt(); } public void skapaGränsSnitt() { this.setSize(400, 200); this.setTitle(“MIwebb, en liten filläsare för Lektion2.");

Lektion 2d container = this.getContentPane(); container.setLayout(new BorderLayout()); nordPanel = new JPanel(); container.add(nordPanel, BorderLayout.NORTH); mittPanel = new JPanel(); mittPanel.setBackground(new Color(255,255,255)); mittPanel.setSize(400, 150); meddelandeRuta = new JTextArea(); mittPanel.add(meddelandeRuta); container.add(mittPanel, BorderLayout.CENTER);

Lektion 2d sydPanel = new JPanel(); sydPanel.setLayout(new GridLayout(1, 2)); infoRuta = new JTextField("Läs in en fil genom att välja Öppna från Arkiv-menyn här ovanför!"); sydPanel.add(infoRuta); container.add(sydPanel, BorderLayout.SOUTH); setJMenuBar(skapaArkivMeny()); setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); }//skapaGränsSnitt

Lektion 2d public JMenuBar skapaArkivMeny() { filBar = new JMenuBar(); arkivMeny = new JMenu("Arkiv"); arkivMeny.setMnemonic('a'); filBar.add(arkivMeny); MenuListener ml = new MenuListener(this, meddelandeRuta, infoRuta); mItem = arkivMeny.add( new JMenuItem("Öppna", 'ö')); mItem.addActionListener(ml); mItem = arkivMeny.add( new JMenuItem("Avsluta", 'a')); mItem.addActionListener(ml); return filBar; }

Lektion 2d public static void main(String[] args) { Lektion2d l2d = new Lektion2d(); }//main }//Lektion2d class MenuListener implements ActionListener { private Lektion2d compo; private JTextArea meddelandeRuta; private JTextField infoRuta;

Lektion 2d public void actionPerformed(ActionEvent e) { String val = e.getActionCommand(); if(val.equals("Öppna")) { öppnaFil(); } else if(val.equals("Avsluta")) System.exit(0); } public void öppnaFil() { JFileChooser jfc = new JFileChooser("./"); jfc.setFileFilter(new InFilter()); int val = jfc.showOpenDialog(compo);

Lektion 2d if(val == JFileChooser.APPROVE_OPTION) { String meddelande = ""; //öppna ström och läs in BufferedReader inFil = null; try { inFil = new BufferedReader(new FileReader(jfc.getSelectedFile())); infoRuta.setText("Inläst från: " + jfc.getSelectedFile()); String rad; while((rad = inFil.readLine()) != null) meddelande += "\n" + rad; }

Lektion 2d }catch (FileNotFoundException fnfe){ System.err.println("Filen hittades inte!"); System.exit(1); }catch (IOException e){ System.err.println("Filen gick inte att läsa!"); System.exit(2); } meddelandeRuta.setText(meddelande); } }

Lektion 2d private class InFilter extends javax.swing.filechooser.FileFilter { public boolean accept(File f) { if(f.getName().toLowerCase().endsWith(".fil")) return true; else return false; } public String getDescription() { return "Visar endast filer med ändelsen.fil"; } }//InFilter }//MenuListener

Lektion 2 Det var allt för idag. Glad Påsk!