Zusammenfassung der Ressource
Java Enterprise
- Spring MVC
- in java classes
- @Controller
- @Autowired
- set the service name
- @RequestMapping (path="/pathname",
method = RequestMethod.POST /
RequestMEthod.GET)
Anmerkungen:
- @RequestMapping(
path = "/add",
method = RequestMethod.POST)
public ModelAndView addstudent(
@RequestParam("name") String name,
@RequestParam("email") String email,
@RequestParam("dob") String dob) {
try {
addStudentService.addstudent(name, email, dob); ModelAndView mv = new ModelAndView("addstudent");
String added = "Student Added!";
mv.addObject("added", added);
return mv;
} catch (Exception ex) {
ModelAndView mv = new ModelAndView("addstudent");
mv.addObject("error", ex.getMessage());
return mv;
- ask autowired service to do something
- servicename.method();
- 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
Anmerkungen:
-
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
Anmerkungen:
- 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);
- client.getOutputStream
- os.write("msg".getbytes());
- os.close();
- Data Streams
Anmerkungen:
- Socket socket = server.accept();
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
while(true)
{
try
{
String mesg = dis.readUTF();
dos.writeUTF("Echo response:"+mesg);
}
catch(EOFException e)
{
break;
}
}
- 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
Anmerkungen:
- 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
Anmerkungen:
- 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
- set JDBCDataSource ds --> setUrl & password &
username --> getConnection()
- conn =
DriverManager.getconnnection(url,username,password)
- PreparedStatement stmt =
conn.prepareStatement("SQL Code")
- stmt.setData(value,value);
- stmt.executeUpdate();
- stmt.close(); conn.close();
- Statement stmt =
conn.createStatement();
- ResultSet rs = stmt.executeQuery("SQL code");
- String name = rs.getString("name");
- rs.close(); stmt.close(); conn.close();
- one conn, many stmt, each stmt one rs
- JPA
- Java Persistence Architecture
- EntityManager em = factory.createEntityManager();
- em.setFlushMode(FlushModeType.COMMIT);
- em.getTransaction().begin();
- em.createQuery("SELECT u FROM User As u").getResultList();
- em.getTransaction().commit();
- factory.close();
- set local dataset item by id
- @Entity / @Table(name = "table
name in database"
Anmerkungen:
- @ID
- setName / getName