1 / 21

2I1073 Lektion 2

2I1073 Lektion 2. Servlets, säkerhet, och filhantering. KTH-MI Peter Mozelius. Lektion 2a. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Lektion2a extends HttpServlet { public void doGet(HttpServletRequest req,

duman
Download Presentation

2I1073 Lektion 2

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. 2I1073 Lektion 2 Servlets, säkerhet, och filhantering KTH-MI Peter Mozelius

  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( "<html xmlns =\"http://www.w3.org/1999/xhtml\">");

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

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

  6. Lektion 2b - web.xml <!--Definition --> • <servlet> <servlet-name> Lektion2a </servlet-name> <servlet-class> Lektion2a </servlet-class> </servlet>

  7. Lektion 2b - web.xml <!--Mappning --> • <servlet-mapping> <servlet-name> Lektion2a </servlet-name> <url-pattern> /Lektion2a </url-pattern> </servlet-mapping> </web-app> 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!

More Related