}
}
performTask() - HttpServletRequest req HttpServletResponse resp.
/* # 5: : RequestOutput.java */
package chapt18;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RequestOutput {
public static void generate(HttpServletResponse resp,
HttpServletRequest req) {
try {
String name, value;
resp.setContentType("text/html; charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("<HTML><HEAD>");
out.print("<TITLE></TITLE>");
out.print("</HEAD><BODY>");
out.print("<TABLE BORDER=3>");
Enumeration names = req.getParameterNames();
while (names.hasMoreElements()) {
name = (String) names.nextElement();
value = req.getParameterValues(name)[0]; /*
name = new String(name.getBytes("ISO-8859-1"), "utf-8");
value = new String(value.getBytes("ISO-8859-1"), "utf-8");
*/
out.print("<TR>");
out.print("<TD>" + name + "</TD>");
out.print("<TD>" + value + "</TD>");
out.print("</TR>");
}
out.print("</TABLE></BODY></HTML>");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
resp text/html UTF-8, . out resp.getWriter(), . HttpServletRequest req Enumeration . , , . ( ) :
, , ISO-8859-1, , UTF-8. getParameterValues(name). .
chapt18,
FormRequest.class
/webapps/FirstProject3/WEB-INF/classes/chapt18
, HTML, chapt18.FormRequest. web.xml :
<servlet>
<servlet-name> MyForm </servlet-name>
<servlet-class> chapt18.FormRequest </servlet-class>
</servlet>
|
|
<servlet-mapping>
<servlet-name> MyForm </servlet-name>
<url-pattern> /testform </url-pattern>
</servlet-mapping>
URL- testform. :
. 18.2.
getParameterValues() . , , . . getParameterNames() Enumeration, , .
, . , doGet() doPost(). service() . , , , synchronized. 500 , , , .
/* # 6: :
ServletSynchronization.java */
package chapt18;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class ServletSynchronization extends HttpServlet {
//
private StringBuffer locked = new StringBuffer();
protected void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
performTask(req, res);
}
private void performTask(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
try {
Writer out = res.getWriter();
out.write(
"<HTML><HEAD>"
+ "<TITLE>SynchronizationDemo</TITLE>"
+ "</HEAD><BODY>");
out.write(createString());
out.write("</BODY></HTML>");
out.flush();
out.close();
} catch (IOException e) {
throw new RuntimeException(
"Failed to handle request: " + e);
}
}
protected String createString() {
//
final String SYNCHRO = "SYNCHRONIZATION";
synchronized (locked) {
try {
for (int i = 0; i < SYNCHRO.length(); i++) {
locked.append(SYNCHRO.charAt(i));
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
String result = locked.toString();
locked.delete(0, SYNCHRO.length() - 1);
return result;
}
}
}
.
. 18.3. Synchronization
. 18.4. Synchronization
, , , .
, , Web-. , , .
|
|
API JavaMail JSP. API JavaMail , . javax.mail.Session , javax.mail.Message , javax.mail.internet.InternetAddress .
. 18.5.
J2EE zip-, http://java.sun.com/products/javamail/, mail.jar activation.jar. jar- (common/lib Tomcat). James, apache.org.
JSP, : to, from, subj
<!-- # 7: : index.jsp-->
<%@ page language="java" contentType=
"text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html><head><title>SendMail Application</title></head>
<b> </b>
< form method="post" action="sendmail" >
<table>
<tr><td><b>To(email):</b></td><td>
<input name="to" type="text" size=40> </td></tr>
<tr><td><b>From(email):</b></td><td>
<input name="from" type="text" size=40> </td></tr>
<tr><td><b>Subject:</b></td><td>
<input name="subj" type="text" size=40> </td></tr>
</table>
<hr>
<textarea name="body" type="text" rows=5 cols=45>
!</textarea>
<br>
<input type="submit" value="!">
</form>
</body></html>
.
/* # 8: :
SendMailServlet.java */
package chapt18;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.activation.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class SendMailServlet
extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
//
private Session mailSession = null;
public void init(ServletConfig config)
throws ServletException {
//mailSession = Session.getDefaultInstance(System.getProperties());
final String host = "mail.smtphost";
final String port = "mail.smtpport";
// web.xml
String hostvalue = config.getInitParameter(host);
String portvalue = config.getInitParameter(port);
java.util.Properties props = new java.util.Properties();
//
props.put(host, hostvalue);
props.put(port, portvalue);
//
mailSession = Session.getDefaultInstance(props, null);
|
|
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//
String from = request.getParameter("from");
String to = request.getParameter("to");
String subject = request.getParameter("subj");
String content = request.getParameter("body");
if ((from == null) || (to == null)
|| (subject == null) || (content == null)) {
/*
*/
response.sendRedirect("index.jsp");
return;
}
//
(new MailSender(to, from, subject, content)).start();
//
response.setContentType("text/html; charset=CP1251");
PrintWriter out = response.getWriter();
out.println("<html><head><title>");
out.println("SendMail Application</title></head>");
out.println(" Message to <b>" + to
+ "</b> sending in progress ");
out.println(" <a href = \"index.jsp\">New message </a>");
out.println("</body></html>");
}
private class MailSender extends Thread {
private String mailTo;
private String mailFrom;
private String mailSubject;
private String mailContent;
MailSender(String mailTo, String mailFrom,
String mailSubject, String mailContent) {
setDaemon(true);
this. mailTo = mailTo;
this. mailFrom = mailFrom;
this. mailSubject = mailSubject;
this. mailContent = mailContent;
}
public void run() {
try {
//
Message message = new MimeMessage(mailSession);
//
message.setFrom(new InternetAddress(mailFrom));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(mailTo));
message.setSubject(mailSubject);
message.setContent(mailContent, "text/plain");
//
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
System. err. print(" ");
} catch (MessagingException e) {
e.printStackTrace();
System. out. print(" ");
}
}
}
}
:
Message to [email protected] sending in progress New message
New message , index.jsp . .
web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>FirstMail</display-name>
<servlet>
<display-name>
SendMailServlet</display-name>
<servlet-name>SendMailServlet</servlet-name>
<servlet-class>
SendMailServlet</servlet-class>
<init-param>
<param-name> mail.smtphost </param-name>
<param-value> mail.bsu.bsu </param-value>
</init-param>
<init-param>
<param-name> mail.smtpport </param-name>
<param-value> 25 </param-value>
</init-param>
|
|
</servlet>
<servlet-mapping>
<servlet-name>chapt18.SendMailServlet</servlet-name>
<url-pattern>/sendmail</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
mail.smtphost mail.attbi.com.
18
Java- JSP-, :
1. : , , .
2. . .
3. , . , .
4. (, , , , , , ..) , .
5. () ().
6. (, , ..) .
7. (, , ..)
.
8.
, . :
) ; ) , ; ) , 0ºC; ) .
9. 21.
10. 34 .
11. .
.
12. . .
13. . , .
.
14. . .
15. -.
16. , 3 , 80 .
B
4 . , , IP- .
18
18.1.
init() URL? ( )
1) ServletConfig.getInitParameter("URL");
2) getServletConfig().getInitParameter("URL");
3) this.getInitParameter("URL");
4) HttpSerlet.getInitParameter("URL");
5) ServletContext.getInitParameter("URL").
18.2.
FirstServlet HTML-?
<html><body>
<a href="/FirstProject/FirstServlettest">OK!</a>
</body><html>
<url-pattern> web.xml :
<url-pattern>/FirstServlettest</url-pattern>
1) doGet();
2) doGET();
3) performTask();
4) doPost();
5) doPOST().
18.3.
init()
1) ;
2) , ;
3) , ;
4) ;
5) ;
6) , , .
18.4.
getResource()
getResourceAsStream() ServletContext?
1) ServletContext ;
2) String InputStream;
3) URL InputStream;
4) URL StreamReader.
18.5.
javax.servlet?
1) ServletRequest;
2) ServletOutputStream;
3) PageContext;
4) Servlet;
5) ServletContextEvent;
|
|
6) .
18.6.
, ? ( )
<HTML><BODY>
<FORM action="/com/MyServlet">
<INPUT type="file" name="filename">
<INPUT type="submit" value="Submit">
</FORM></BODY></HTML>
1) request.getParameterValues("filename");
2) request.getAttribute("filename");
3) request.getInputStream();
4) request.getReader();
5) request.getFileInputStream().
Java Server Pages
Java Server Pages (JSP) Sun Microsystems, .
, JSP HTML, XML, WML .
/ JSP, Web-:
1) , : , JSP, java-;
2) , HTML, JavaScript, ..
, HTTP-, (Servlet Engine) .
: Web-, , Web-, .
JSP- :
d) .
Web- Web- .
d) .
JSP-, Java, , JSP Web-. JSP .
e) .
JavaBeans Enterprise JavaBeans (EJB) , Web-.
f) .
JSP , , JSP JavaScript, HTML-. JavaScript , HTML-. JavaBean .
, JSP , JavaBean JSP.
Java Server Pages ( HTML, JSP ) -. , , JSP. , JSP/Servlet-, thin-client ( ), .
JSP HTML-, JSP-, action-, JSTL . JSP 2.0 :
<%@ %> JSP.
<%! %> Java , expression- . out , .
<% %> Java- JSP-. . , , . Java _jspService() .
<%= %> Java, , String out.
<%-- JSP- --%>, JSP- .
action
, , . JSP 2.0. JSP :
1) jsp:declaration , <%!... %>;
2) jsp:scriptlet , <%... %>;
3) jsp:expression , <%=... %>;
4) jsp:text ;
2 jsp:useBean Java Bean. , page (), request (), session () application (). , , id ( ), scope ( ), class ( ), type ( class).
<jsp:useBean id=ob
scope=session
class=test.MyBean />
ob MyBean, . JavaBean , field, field, , , setField(type value), getField().
package test;
public class MyBean {
private String field = " ";
public String getField() {
return info;
}
public void setField(String f) {
field = f;
}
}
5) jsp:setProperty name . property *, JavaBean , - (setter-) :
<jsp:setProperty name=ob
property=field
value= />
e) jsp:getProperty , out:
<jsp:getProperty name=ob property=field />
f) jsp:include :
<jsp:include page= URL
flush=true/>
g) jsp:forward :
<jsp:forward page= URL />
h) jsp:plugin <OBJECT> <EMBED>, , Java Bean.
i) jsp:params jsp:plugin.
6) jsp:param , forward, include, plugin.
7) jsp:fallback , , . plugin.
:
<jsp:plugin type=bean | applet
code=test.com.ReadParam
width=250
height=250>
<jsp:params>
<jsp:param name=bNumber value=7 />
<jsp:param name=state value=true />
</jsp:params>
<jsp:fallback>
<p> unable to start plugin </p>
</jsp:fallback>
</jsp:plugin>
5 11, , , /WEB-INF, /classes.
<jsp:attribute>, <jsp:body>, <jsp:invoke>, <jsp:doBody>, <jsp:element>, <jsp:output> .
JSP-
JSP- JSP- XML-, , JSP, JSTL, XML- JSP. JSP- , . JSP- .jspx.
taglib JSP:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
JSP-:
<jsp:root xmlns:c="http://java.sun.com/jsp/jstl/core"/>
page JSP:
<%@ page contentType="text/html"%>
JSP-:
<jsp:directive.page contentType="text/html" />
include JSP:
<%@ include file="file.jspf"%>
JSP-:
<jsp:directive.include file="file.jspf" />
, JSP- JSP-.
<!-- # 1: jsp-: page.jsp -->
<%@ page contentType="text/html; charset=Cp1251" %>
<html><head><title>JSP-</title></head>
<%! private int count = 0;
String version = new String("J2EE 1.5");
private String getName(){ return "J2EE 1.6";} %>
<% out.println(" count: "); %>
<%= count++ %>
<br/>
<% out.println(" count : " + count); %>
<br/>
<% out.println(" version: "); %>
<%= version %>
<br/>
<% version=getName();
out.println(" version: " + version); %>
</html>
JSP- , , .
<!-- # 2: jsp-: page.jspx -->
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
version="2.0">
<jsp:directive.page contentType=
"text/html; charset=UTF-8" />
<html><body>
<jsp:declaration>
private int count = 0;
String version = new String("J2EE 1.5");
private String getName(){ return "J2EE 1.6";}
</jsp:declaration>
<jsp:scriptlet>
out.println(" count: ");
</jsp:scriptlet>
<jsp:expression>
count++
</jsp:expression>
<br />
<jsp:scriptlet>
out.println(" count :"
+ count);
</jsp:scriptlet>
<br/>
<jsp:scriptlet>
out.println(" version: "); </jsp:scriptlet>
<jsp:expression> version </jsp:expression>
<br/>
<jsp:scriptlet> version=getName();
out.println(" version: " + version);
</jsp:scriptlet>
</body></html>
</jsp:root>
JSP-.
JSTL
JSP-, , action ( ) , JSTL (JSP Standard Tag Library). JSTL Java- JSP. , , , -, Java.
JSTL 1.1.2 (jstl-1.1.2.jar standard-1.1.2.jar) c apache.org. /lib . xmlns root ( JSP- taglib uri="") , , JSP- JSTL. uri ( xmlns) ( c.tld). JSP JSTL, :
<!-- # 3: jsp-: simple.jspx -->
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c=http://java.sun.com/jsp/jstl/core
version="2.0">
<jsp:directive.page contentType=
"text/html; charset=Utf-8"/>
<html><body>
<c:out value="Welcome to JSTL"/>
</body></html>
</jsp:root>
<c:out/> value JspWriter.
JSTL :
- Expression Language, ;
- , , ;
- (URL) ;
- JSP;
- ;
- XML, .
Expression Language
JSTL Expression Language (EL). EL , (page, request, application) .
EL ${ }.
JSP 2.0 / JSTL 1.1, EL JSP . web-app 2.4 isELIgnored true. page true.
EL- , PageContext.findAttribute( ). : page (PageContext), request (HttpServletRequest), session (HttpSession), application (ServletContext). , null. paramValues requestHeaders.
, , , JavaBeans, , List, Map, Array . EL . []. . :
<c:out value=${student.name}/>
<!-- # 4: jsp-: simple2.jspx -->
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:c="http://java.sun.com/jsp/jstl/core"
version="2.0">
<jsp:directive.page contentType=
"text/html; charset=Utf-8"/>
<html>
<head><title> EL</title></head>
<body>
<c:set var="login" value="" scope="page"/>
<c:out value="${login} in Rio"/>
<br/>
<c:out value=" : ${login.bytes} "/>
</body></html>
</jsp:root>
. , . login.bytes Java login.getBytes().
:
in Rio
: [B@edf730
EL .
:
:
== ( eq), != ( neq), < ( lt), > ( gt), <= ( le), >= ( ge).
: +, -, *, / ( div), % ( mod).
: && ( and), || ( or),! ( not).
empty null, . . , .
:
<c:if test=${ not empty user and user.name neq guest}>
User is Customer.
</c:if>