ModelAndView mv = new ModelAndView("jsp location");
@Service
pass action to JPA or JDBC, get result /
throw exception
import springframework
in JSP
<%@ taglib prefix="c" uri="java.sun.com....."
<c:if test="${error != null}">
<c: forEach var="st"
items="${studentlist}">
Server/Client
Socket
programming
Basic serversocket
Server side
Anotações:
import java.io.IOException;
import java.net.ServerSocket;
public class Server {
public static void main(String[] args)
{
ServerSocket server = null;
try
{
server = new ServerSocket(5000); } catch(IOException e) { e.printStackTrace(); } }}
while(true)
{
try
{
Socket socket = server.accept();
InputStream is = socket.getInputStream();
StringBuffer mesg = new StringBuffer();
while(true)
{
int data = is.read();
if (data == -1)
{
break;
}
else
{
mesg.append((char)data);
}
System.out.println(mesg);
}
catch(IOException e)
{
e.printStackTrace();
}
}
use socket.getInputStream
use socket = server.accept();
open server by ServerSocket(5000)
decode by StringBuffer
is.read();
Client side
Anotações:
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args)
{
try
{
Socket client = new Socket("localhost",5000);
OutputStream os = client.getOutputStream();
os.write("Hello ISCG7425".getBytes());
os.close();
client.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Socket client = new Socket("localhost",5000);
DataOutputStream dos = new DataOutputStream(client.getOutputStream());
DataInputStream dis = new DataInputStream(client.getInputStream());
dos.writeUTF("Test Message");
String resp = dis.readUTF();
System.out.println(resp);
client.close();
use DataInputStream/DataOutputStream
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
dis.readUTF();
dos.writeUTF("msg");
Server Threads
Anotações:
final Socket socket = server.accept();
Thread serverThread = new Thread() {
public void run() {
try {
InputStream is = socket.getInputStream();
StringBuffer mesg = new StringBuffer();
while(true) {
int data = is.read();
if(data == -1)
break;
else
mesg.append((char)data);
}
System.out.println(this.getName() + "\n"+ mesg);
}catch(IOException e) { /* … Handle exceptions … / }
}
};
serverThread.start();
Thread serverThread = new Thread(){the server activities}
serverTread.start();
should include thread.sleep(1000) method
Servlets and JSP
http
text-base protocol for hypertext (HTML)
20+ years old
becomes popular and extended to
send other type of data
simple to implement
request-respond kind of protocol,
only client can request
Servlets
help developer focus on business rules
providing resources to handle incoming
request and outgoing responds
need mapping with html url-pattern
can be done by annotations: @WebServlet("/weblocation")
JSP
JavaServerPages
need not to transfer java to html
translated to Servlets
using <% jave coding %> format to
inport code to webpage
can directly use some variables:request /
response / out / session / application
Database connection
JDBC
Anotações:
JDBCDataSource ds = new JDBCDataSource();
// setup URL according to HSQLDB specs
ds.setUrl("jdbc:hsqldb:hsql://localhost/contacts");
// set other data source properties
ds.setPassword("");
ds.setUser("SA");
// setup global database connection
conn = ds.getConnection();
interface to allow java to access SQL database
use Classes and Factory method
Driver based model
basic setup: port, hostname, schema, database name