Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "2I1073 Lektion 2 KTH-MI Peter Mozelius Servlets, säkerhet, och filhantering."— Presentation transcript:

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

2 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 {

3 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( "\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"); out.println( " ");

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

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

6 Lektion 2b - web.xml - Lektion2a Lektion2a

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

8 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;

9 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); }

10 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

11 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;

12 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.");

13 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);

14 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

15 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; }

16 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;

17 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);

18 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; }

19 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); } }

20 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

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


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

Similar presentations


Ads by Google