MVC in JSP (was: template in servlet)



Servlet = viel Java mit wenig HTML
JSP = viel HTML mit wenig Java ... Bean-Zugriffe ...
Bean = nur Java

also ich habe sehr viel Java und sehr viel html .

Dafuer eignet sich:´
HTML ins JSP,
Java in die Java-Beans

au�erdem sollte der html-Teil vom Designer jederzeit verändert werden
können.

Ja, das ist der Vorteil von JSP,
dass es mit HTML-Editoren gewartet werden kann
und jedenfalls von Personen,
die sich mit der Anzeige (View) und HTML auskennen,
sich aber mit der Datenverarbeitung (Model) und Java nicht auskennen
muessen.

dass ich den html-code in ein eigenes file packe
und den Java-code in ein eigenes file packe.

.... mit nur ein paar JSP-Tags im HTML-Code drin,
ja, genau das ist JSP!

im html-file sind Variablen, die dann befüllt werden und
am Ende der html-code verschickt wird.

ja, genau, diese "Variablen" sind die so genannten Beans,
die in JSP zwischen dem HTML-Code
mit <jsp:usebean ... > Tags angelegt,
mit <jsp:setproperty ...> Tags befuellt und
mit <jsp:getproperty ...> Tags verwendet werden.

Ich zeig Dir einmal ein ganz primitives Beispiel,
das aus 3 Dateien besteht
(1 Java ohne HTML, 1 HTML ohne Java, 1 JSP ohne Java):


Die Java-Datei "QuadratBean.java":
----------------------------------

package packagename;
public class QuadratBean {

private String user;
private int zahl;

public void setUser( String user ) {
this.user = user;
}
public String getUser() {
return user;
}

public void setZahl( int zahl ) {
this.zahl = zahl;
}
public int getZahl() {
return zahl;
}

public int getQuadrat() {
return zahl*zahl;
}
}

Die HTML-Datei Eingabeformular.html:
------------------------------------

<html>
....
<form action="http://...../quadrat.jsp"; method="GET">
<p>Username: <input type="text" name="user" />
<p>Zahl: <input type="text" name="zahl" />
<p>Quadrat berechnen: <input type="submit" />
</form>
....
</html>


Die JSP-Datei quadrat.jsp:
--------------------------

<html>
<head>
<title>Quadrat</title>
<jsp:usebean id="quad" class="packagename.QuadratBean" >
<jsp:setproperty name="quad" property="*" />
</jsp:usebean>
</head>
<body>
<p>Lieber <jsp:getproperty name="quad" property="user" />!
<p>Hier ist das Ergenbnis der Berechung:
<p>Zahl = <jsp:getproperty name="quad" property="zahl" />!
<p>Quadrat = <jsp:getproperty name="quad" property="quadrat" />!
</body>
</html>


Ich denke, was das tut, kannst Du aus dem Code selbst erraten.

--


.



Relevant Pages

  • Re: soooo many questions!
    ... takes to create a dropdown HTML menu in Java, ... as a string via the toString ...
    (comp.lang.java.beans)
  • Re: OutOfMemoryError - how to find root cause
    ... If you extract a small substring of a large string in any of these ways, you may be fooled into thinking that it uses less heap than it really does. ... That causes a new, right-sized char array to be created for the new String, and it is, IMO, the only reason to ever use the Stringconstructor. ... because only one instance of a JSP not declared non-thread-safe will ever be maintained by the application at one time. ... I find that it helps to keep in mind that JSP is just a shortcut to Java code; if I know how JSP constructs and actions map to Java then I can predict a JSP's fine behavioral details. ...
    (comp.lang.java.programmer)
  • Re: Java Beginner
    ... Let's say that I have a log in page, created with only HTML code. ... Now in my jsp page I check the validity of my log-in ... --I show all the clients for this Id ... If it's java you're particularly interested in have a look at the struts ...
    (comp.lang.java.programmer)
  • Re: Java Beginner
    ... Let's say that I have a log in page, created with only HTML code. ... Now in my jsp page I check the validity of my ... --I show all the clients for this Id ... If it's java you're particularly interested in have a look at the struts ...
    (comp.lang.java.programmer)
  • Re: JSP and Servlet
    ... > Anyone can tell me what is the difference between JSP and Servlet? ... Servlets are written in pure Java. ... Requests for a web page go to a Java ... JSP's are HTML pages with some Java hacked into them. ...
    (comp.lang.java.programmer)