<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Der Blog von Benny Neugebauer &#187; Code Snippets</title>
	<atom:link href="http://www.bennyn.de/tag/code-snippets/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bennyn.de</link>
	<description>Alles über die Informatik &#38; Co.</description>
	<lastBuildDate>Mon, 06 Feb 2012 11:49:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java XML SAX Parser</title>
		<link>http://www.bennyn.de/programmierung/java/java-xml-sax-parser.html</link>
		<comments>http://www.bennyn.de/programmierung/java/java-xml-sax-parser.html#comments</comments>
		<pubDate>Fri, 04 Feb 2011 06:02:30 +0000</pubDate>
		<dc:creator>bennyn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[JAX]]></category>
		<category><![CDATA[JSE]]></category>
		<category><![CDATA[Parser]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.bennyn.de/?p=2296</guid>
		<description><![CDATA[Um mit Java eine XML Datei zu parsen, empfehle ich einen SAX Parser, da dieser schneller ist als ein DOM Parser. Die Implementierung eines SAX Parsers in Java ist einfach. Man muss nur eine routinemäßige Initialisierung vornehmen und einen eigenen Handler schreiben. Wie das funktioniert, zeigt mein nachfolgendes Code-Beispiel. Ich erkläre ebenfalls, wie man den [...]]]></description>
			<content:encoded><![CDATA[<p>Um mit Java eine <strong>XML Datei</strong> zu parsen, empfehle ich einen <strong>SAX Parser</strong>, da dieser schneller ist als ein DOM Parser. Die Implementierung eines SAX Parsers in <strong>Java</strong> ist einfach. Man muss nur eine routinemäßige Initialisierung vornehmen und einen eigenen Handler schreiben. Wie das funktioniert, zeigt mein nachfolgendes Code-Beispiel. Ich erkläre ebenfalls, wie man den SAX Parser in Google <strong>Android</strong> verwenden kann!<br />
<span id="more-2296"></span></p>
<h2>XML-Datei</h2>
<p>Ich möchte gerne alle Apfelsorten aus der Datei <b>apples.xml</b> auslesen:</p>
<p><b>apples.xml</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Golden Delicious<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Granny Smith<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>	
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Idared<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>	
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Fuji<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Ginger Gold<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>McIntosh<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Northern Spy<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Pink Pearl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Pinova<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Red Delicious<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Spitzenberg<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>		
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Summerfree<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>SweeTango<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cultivar<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Um mein Ziel zu erreichen, muss mein SAX-Parser immer dann, wenn ein öffnendes <code>name</code>-Tag gefunden wird, anfangen alle nachfolgenden Zeichen zu puffern. Die Pufferung muss beendet werden, sobald ein schließendes <code>name</code>-Tag gefunden wird. Der Inhalt des Puffers ist dann mein Ergebnis, welches ich in einer <code>ArrayList</code> aufbewahren möchte.</p>
<h2>Code:</h2>
<p><b>Main.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">myjaxparser</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> appleCultivars <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">File</span> file <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;apples.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Parse apples.xml</span>
    <span style="color: #000000; font-weight: bold;">try</span>
    <span style="color: #009900;">&#123;</span>
      MySaxParser parser <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MySaxParser<span style="color: #009900;">&#40;</span>file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parser.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      appleCultivars <span style="color: #339933;">=</span> parser.<span style="color: #006633;">getAppleCultivars</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// Print results</span>
      <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> appleCultivars.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>appleCultivars.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> ex<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Another exciting error occured: &quot;</span> <span style="color: #339933;">+</span> ex.<span style="color: #006633;">getLocalizedMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><b>MySaxParser.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">myjaxparser</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.MalformedURLException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.parsers.ParserConfigurationException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.parsers.SAXParser</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.parsers.SAXParserFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.xml.sax.InputSource</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.xml.sax.SAXException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.xml.sax.XMLReader</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MySaxParser
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">URL</span> url<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> appleCultivars<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> MySaxParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> MySaxParser<span style="color: #009900;">&#40;</span><span style="color: #003399;">File</span> file<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">MalformedURLException</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">url</span> <span style="color: #339933;">=</span> file.<span style="color: #006633;">toURI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> parse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ParserConfigurationException, SAXException, <span style="color: #003399;">IOException</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Initialize SAX Parser:</span>
    SAXParserFactory factory <span style="color: #339933;">=</span> SAXParserFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    SAXParser parser <span style="color: #339933;">=</span> factory.<span style="color: #006633;">newSAXParser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    XMLReader reader <span style="color: #339933;">=</span> parser.<span style="color: #006633;">getXMLReader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Create SAX Handler:</span>
    MySaxHandler handler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MySaxHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    reader.<span style="color: #006633;">setContentHandler</span><span style="color: #009900;">&#40;</span>handler<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Parse XML file:</span>
    InputSource input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> InputSource<span style="color: #009900;">&#40;</span>url.<span style="color: #006633;">openStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    reader.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Get the result:</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">appleCultivars</span> <span style="color: #339933;">=</span> handler.<span style="color: #006633;">getAppleCultivars</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> getAppleCultivars<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">appleCultivars</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><b>MySaxHandler.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">myjaxparser</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.xml.sax.Attributes</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.xml.sax.SAXException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.xml.sax.helpers.DefaultHandler</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MySaxHandler <span style="color: #000000; font-weight: bold;">extends</span> DefaultHandler
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">StringBuffer</span> buffer<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> buffering<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> appleCultivars<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> MySaxHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buffer</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buffering</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    appleCultivars <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> startDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SAXException
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buffer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> startElement<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> namespaceURI, <span style="color: #003399;">String</span> localName, <span style="color: #003399;">String</span> tagName, <span style="color: #003399;">Attributes</span> attributes<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SAXException
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> tag <span style="color: #339933;">=</span> tagName<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tag.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buffering</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> endElement<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> namespaceURI, <span style="color: #003399;">String</span> localName, <span style="color: #003399;">String</span> tagName<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> SAXException
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> tag <span style="color: #339933;">=</span> tagName<span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> tagValue <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tag.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      tagValue <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buffer</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buffering</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buffer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringBuffer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    parseValue<span style="color: #009900;">&#40;</span>tagValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  @Override
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> characters<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span> chars<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>, <span style="color: #000066; font-weight: bold;">int</span> start, <span style="color: #000066; font-weight: bold;">int</span> length<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buffering</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buffer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">buffer</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>chars, start, length<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> parseValue<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">appleCultivars</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> getAppleCultivars<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">appleCultivars</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><b>Achtung Android!</b><br />
Für den Einsatz von <code>MySaxHandler.java</code> in Android muss man <code>String tag = tagName;</code> gegen <code>String tag = localName;</code> austauschen. Das ist zwar ziemlich unlogisch, jedoch hat das Code-Beispiel nur so bei mir funktioniert (<i>getestet mit Google Android 2.1</i>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bennyn.de/programmierung/java/java-xml-sax-parser.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Andere JSPs in Scriptlets einfügen</title>
		<link>http://www.bennyn.de/programmierung/java/andere-jsps-in-scriptlets-einfugen.html</link>
		<comments>http://www.bennyn.de/programmierung/java/andere-jsps-in-scriptlets-einfugen.html#comments</comments>
		<pubDate>Sun, 26 Sep 2010 00:44:28 +0000</pubDate>
		<dc:creator>bennyn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[einfügen]]></category>
		<category><![CDATA[JEE]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Scriptlet]]></category>

		<guid isPermaLink="false">http://www.bennyn.de/?p=1961</guid>
		<description><![CDATA[Wie man andere JavaServer Pages in einer JavaServer Page einfügen kann, zeigt das folgende Code-Stückchen. Es handelt sich dabei um ein Scriptlet, das bei Vorhandensein eines User-Objektes ein Logout-Formular anzeigt. scriptlet.jsp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 &#60;%@page import=&#34;util.SecurityUtil, persistence.User&#34; %&#62; &#60;% SecurityUtil security [...]]]></description>
			<content:encoded><![CDATA[<p>Wie man andere <strong>JavaServer Pages</strong> in einer JavaServer Page einfügen kann, zeigt das folgende Code-Stückchen. Es handelt sich dabei um ein <strong>Scriptlet</strong>, das bei Vorhandensein eines User-Objektes ein Logout-Formular anzeigt.</p>
<p><strong>scriptlet.jsp</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@page <span style="color: #000000; font-weight: bold;">import</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;util.SecurityUtil, persistence.User&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>
	SecurityUtil security <span style="color: #339933;">=</span> SecurityUtil.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	User user <span style="color: #339933;">=</span> security.<span style="color: #006633;">getUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>user <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
<span style="color: #339933;">%&gt;</span>
	<span style="color: #339933;">&lt;%</span>@include file<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;form-logout.jsp&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">else</span>
	<span style="color: #009900;">&#123;</span>
<span style="color: #339933;">%&gt;</span>
	<span style="color: #339933;">&lt;%</span>@include file<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;form-login.jsp&quot;</span> <span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;%</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">%&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bennyn.de/programmierung/java/andere-jsps-in-scriptlets-einfugen.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Datei zeilenweise auslesen in PHP</title>
		<link>http://www.bennyn.de/programmierung/php/datei-zeilenweise-auslesen-in-php.html</link>
		<comments>http://www.bennyn.de/programmierung/php/datei-zeilenweise-auslesen-in-php.html#comments</comments>
		<pubDate>Wed, 15 Sep 2010 10:26:40 +0000</pubDate>
		<dc:creator>bennyn</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[auslesen]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Datei]]></category>
		<category><![CDATA[zeilenweise]]></category>

		<guid isPermaLink="false">http://www.bennyn.de/?p=1819</guid>
		<description><![CDATA[Mit diesem Code kann man mit PHP eine Textdatei zeilenweise auslesen und anzeigen: 1 2 3 4 5 6 7 &#60;?php $file = fopen&#40;&#34;namensliste.txt&#34;,&#34;r&#34;&#41;; while&#40;$row = fgets&#40;$file, 4096&#41;&#41; &#123; echo $row.&#34;&#60;br/&#62;&#34;; &#125; ?&#62; Möchte man eine zufällige Zeile auslesen, so geht das wie folgt: 1 2 3 4 5 6 7 8 9 10 &#60;?php [...]]]></description>
			<content:encoded><![CDATA[<p>Mit diesem <strong>Code</strong> kann man mit <strong>PHP</strong> eine <strong>Textdatei zeilenweise auslesen</strong> und anzeigen:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;namensliste.txt&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Möchte man eine <strong>zufällige Zeile auslesen</strong>, so geht das wie folgt:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;namensliste.txt&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
&nbsp;
	<span style="color: #000088;">$randomRow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$randomRow</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bennyn.de/programmierung/php/datei-zeilenweise-auslesen-in-php.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zufallszahlen in Java</title>
		<link>http://www.bennyn.de/programmierung/java/zufallszahlen-in-java.html</link>
		<comments>http://www.bennyn.de/programmierung/java/zufallszahlen-in-java.html#comments</comments>
		<pubDate>Sun, 14 Mar 2010 22:19:39 +0000</pubDate>
		<dc:creator>bennyn</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Numbers]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[Zufallszahlen]]></category>

		<guid isPermaLink="false">http://www.bennyn.de/programmierung/java/zufallszahlen-in-java.html</guid>
		<description><![CDATA[Um Zufallszahlen in Java zu erstellen, kann man die Bibliothek java.util.Random verwenden. Die Zufalls-Klasse von Java ist sehr vielseitig und bietet unter anderem die Möglichkeit, Zufallszahlen in einem bestimmten Bereich zu definieren. An dieser Stelle folgt ein kleiner Beispiel-Code, welcher 20 Zufallszahlen im Bereich von 0 bis 72 ausgibt: 1 2 3 4 5 6 [...]]]></description>
			<content:encoded><![CDATA[<p>Um <b>Zufallszahlen in Java</b> zu erstellen, kann man die Bibliothek <i>java.util.Random</i> verwenden. Die Zufalls-Klasse von Java ist sehr vielseitig und bietet unter anderem die Möglichkeit, Zufallszahlen in einem bestimmten Bereich zu definieren. An dieser Stelle folgt ein kleiner Beispiel-Code, welcher 20 Zufallszahlen im Bereich von 0 bis 72 ausgibt:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> erstelleZufallsZahl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Random</span> zufallsgenerator <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">int</span> zahl <span style="color: #339933;">=</span> zufallsgenerator.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">72</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>zahl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Etwas ausführlicher:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Random</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JavaApplication1
<span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    erstelleZufallsZahl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> erstelleZufallsZahl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Random</span> zufallsgenerator <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">int</span> zahl <span style="color: #339933;">=</span> zufallsgenerator.<span style="color: #006633;">nextInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">72</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>zahl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bennyn.de/programmierung/java/zufallszahlen-in-java.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java Server Pages &#8211; Beispiel</title>
		<link>http://www.bennyn.de/aktuelles/java-server-pages-beispiel.html</link>
		<comments>http://www.bennyn.de/aktuelles/java-server-pages-beispiel.html#comments</comments>
		<pubDate>Thu, 11 Mar 2010 14:07:01 +0000</pubDate>
		<dc:creator>bennyn</dc:creator>
				<category><![CDATA[Aktuelles]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[Beispiel]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Java Server Pages]]></category>
		<category><![CDATA[JEE]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Servlets]]></category>

		<guid isPermaLink="false">http://www.bennyn.de/?p=840</guid>
		<description><![CDATA[Es folgt ein Beispiel zur Erstellung einer Java Server Page (kurz JSP). Die Java Server Page wird durch ein Servlet (im Beispiel NewServlet.java) aufgerufen. Daher muss im Webbrowser auch die Adresse des Servlets eingegeben werden. Als Parameter wird dabei das gewünschte Command mit einer dazugehörigen Action ausgeführt. Im Beispielaufruf heißt das Command &#8220;cmd&#8221; und die [...]]]></description>
			<content:encoded><![CDATA[<p>Es folgt ein Beispiel zur Erstellung einer <b>Java Server Page</b> (<i>kurz <b>JSP</b></i>).</p>
<p>Die <b>Java Server Page</b> wird durch ein <b>Servlet</b> (im Beispiel <i>NewServlet.java</i>) aufgerufen. Daher muss im Webbrowser auch die Adresse des Servlets eingegeben werden. Als Parameter wird dabei das gewünschte <i>Command</i> mit einer dazugehörigen <i>Action</i> ausgeführt.</p>
<p>Im Beispielaufruf heißt das Command &#8220;cmd&#8221; und die Action &#8220;actionman&#8221;:</p>
<p>http://localhost:8080/Projekt/servlet?do=actionman</p>
<p><u>Quellcode:</u></p>
<p>Projektname > &#8220;Source Packages&#8221; > controller.web > <b>NewServlet.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">controller.web</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletConfig</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.annotation.WebServlet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
&nbsp;
@WebServlet<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;NewServlet&quot;</span>, urlPatterns<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;/servlet&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NewServlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServletControllerBase
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span>ServletConfig conf<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException
    <span style="color: #009900;">&#123;</span>
        HttpRequestActionBase action <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        actions <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Action: Möglicher Wert (Value) für einen Steuerbefehl (Command)</span>
        action <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MyAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        actions.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;actionman&quot;</span>, action<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Weitere &quot;action&quot; erstellen und &quot;actions&quot; hinzufügen...</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> getOperation<span style="color: #009900;">&#40;</span>HttpServletRequest req<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Command: Steuerbefehl</span>
        <span style="color: #000000; font-weight: bold;">return</span> req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;do&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Projektname > &#8220;Source Packages&#8221; > controller.web > <b>MyAction.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">controller.web</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.RequestDispatcher</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyAction <span style="color: #000000; font-weight: bold;">extends</span> HttpRequestActionBase
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> perform<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> ServletException
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">String</span> strTemp <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Willkommen auf der JSP-Seite.&quot;</span><span style="color: #339933;">;</span>
            req.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;willkommen&quot;</span>, strTemp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            RequestDispatcher reqDisp <span style="color: #339933;">=</span> req.<span style="color: #006633;">getRequestDispatcher</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;showaction.jsp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            reqDisp.<span style="color: #006633;">forward</span><span style="color: #009900;">&#40;</span>req, resp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Projektname > &#8220;Web Pages&#8221; > <b>showaction.jsp</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;%</span>@page contentType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html&quot;</span> pageEncoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #339933;">%&gt;</span>
<span style="color: #339933;">&lt;!</span>DOCTYPE <span style="color: #003399;">HTML</span> <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #0000ff;">&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span>
   <span style="color: #0000ff;">&quot;http://www.w3.org/TR/html4/loose.dtd&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>html<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>head<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>meta http<span style="color: #339933;">-</span>equiv<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Content-Type&quot;</span> content<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;</span>title<span style="color: #339933;">&gt;</span>JSP Page<span style="color: #339933;">&lt;/</span>title<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>head<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;</span>body<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;!--</span> Variante <span style="color: #cc66cc;">1</span> <span style="color: #339933;">--&gt;</span>
        <span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;&lt;%=</span>request.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;willkommen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%&gt;&lt;/</span>h1<span style="color: #339933;">&gt;</span>
        <span style="color: #339933;">&lt;!--</span> Variante <span style="color: #cc66cc;">2</span> <span style="color: #339933;">--&gt;</span>
        <span style="color: #339933;">&lt;</span>h1<span style="color: #339933;">&gt;</span>$<span style="color: #009900;">&#123;</span>hallo<span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>h1<span style="color: #339933;">&gt;</span>
    <span style="color: #339933;">&lt;/</span>body<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>html<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><u>Benötigte Ressourcen:</u><br />
Projektname > &#8220;Source Packages&#8221; > controller.web > <b>HttpServletControllerBase.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">controller.web</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletConfig</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServlet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> HttpServletControllerBase <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">HashMap</span> actions<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span>ServletConfig conf<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException
        <span style="color: #009900;">&#123;</span>
            HttpRequestActionBase action <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            actions <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException
        <span style="color: #009900;">&#123;</span>
	    <span style="color: #003399;">String</span> op <span style="color: #339933;">=</span> getOperation<span style="color: #009900;">&#40;</span>req<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    HttpRequestActionBase action <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HttpRequestActionBase<span style="color: #009900;">&#41;</span>actions.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>op<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    action.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>req, resp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException
        <span style="color: #009900;">&#123;</span>
&nbsp;
	    <span style="color: #666666; font-style: italic;">// Zunaechst wird die URL analysiert,</span>
            <span style="color: #666666; font-style: italic;">// um die Operation, die ausgefueht werden soll zu bestimmen</span>
	    <span style="color: #003399;">String</span> op <span style="color: #339933;">=</span> getOperation<span style="color: #009900;">&#40;</span>req<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #666666; font-style: italic;">// dann wird die entsprechende Aktion aus der Map geholt ...</span>
	    HttpRequestActionBase action <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HttpRequestActionBase<span style="color: #009900;">&#41;</span>actions.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>op<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #666666; font-style: italic;">// ... und angestossen</span>
	    action.<span style="color: #006633;">perform</span><span style="color: #009900;">&#40;</span>req, resp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/** Methode muss noch definiert werden, um die Kennung der
	  * Operation aus der URL zu lesen
	  * @param req Http-Request
	  * @return Name der Aktion, die ausgefuehrt werden soll
	  */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #003399;">String</span> getOperation<span style="color: #009900;">&#40;</span>HttpServletRequest req<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Projektname > &#8220;Source Packages&#8221; > controller.web > <b>HttpRequestActionBase.java</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">controller.web</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.RequestDispatcher</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> HttpRequestActionBase
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> perform<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> ServletException<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> forward<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse resp, <span style="color: #003399;">String</span> forwardName<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span>
    <span style="color: #009900;">&#123;</span>
        RequestDispatcher reqDis <span style="color: #339933;">=</span> req.<span style="color: #006633;">getRequestDispatcher</span><span style="color: #009900;">&#40;</span>forwardName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        reqDis.<span style="color: #006633;">forward</span><span style="color: #009900;">&#40;</span>req, resp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.bennyn.de/aktuelles/java-server-pages-beispiel.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

