一 简单的例子
first.xml
<html>
<head>
<title>The First One html text£¡</title>
</head>
<body>¹þ¹þžœŒþ</br>JDK1.6.30</br>
<a href="second.html" target="_blank"> Second Page</a>
</body>
</html>
second.xml
<html>
<head>
<title> The Second Html Page. </title>
</head>
<body>
<a href="http://www.baidu.com">Baidu</a>
</body>
</html>
form.xml
<html>
<head>
<title> Forms </title>
</head>
<body>
List normal forms to you.<br>
<form action="first.html" method="GET">
Choose name <input type="text" name="text"><br>
Choose sex <input type="radio" name="sex" value="male"> Male
<input type="radio" name="sex" value="female"> Female <br>
Choose sports <input type="checkbox" name="sport" value="swimming">Swimming
<input type="checkbox" name="sport" value="basketball">BasketBall
<input type="checkbox" name="sport" value="football">FootBall<br>
Choose city <select name="city">
<option value="bj">BeiJing</option>
<option value="sh">ShangHai</option>
<option value="wh">WuHan</option>
</select><br>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
</form>
</body>
</html>
Css
<div align="left|middle|right" ></div>
<span style="font-family:heiti;font-size:10pt;font-weight:*;"></span>
<span style="cursor:*"></span>
Jsp
1 Base Jsp Syntax:1> comment
<%-- --%> jsp
<%/* */%> java
<%// %>
<!-- --> html can be looked.
2> declaration
<%! declaration;[declaration;]...... %>
3> expression
<%= expression %>
4> directive
<%@ page %>
<%@ include %>
<%@ taglib %>
5> action
<jsp:include page="" flush="true|false"/>
<jsp:forward page="" />
<jsp:param name="" value="" />
<jsp:useBean id="" class="" scope="" />
<jsp:setProperty name="" property="" value="" />
<jsp:getProperty name="" property="" />
2 Jsp Inter Object
request
response
out
session
application
pageContext
config
page
exception
Servlet
1 Servlet as JavaClass run in server , and own its lifespan.
2 Its lifespan:
Servlet Object(run in a specified colletion)
init()
Client --request-->
service()
<--response--
destroy()
3 Its using method:
public class A extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse responce) throws ServletException,IOException
public void doPost(HttpServletRequest request,HttpServletResponse responce) throws ServletException,IOException
}
Servlet config data in web.xml;
4 HttpServlet method:
init();
service();
destroy();
GetServletConfig();
GetServletInfo();
doGet();
doPost();
......
5 File Operater:
Read:
PriterWriter out = response.getWriter();
String fileName = "**.*";
String filePath = request.getRealPath(fileName);
File file = new File(filePath);
if(file.exists()){
FileReader freader = new FileReader(file);
BufferReader br = new BufferReader(freader);
String line = null;
while((line = br.readLine()) != null){
//......
}
}
else{
//......
}
Write:
String newFile = "new.*";
String newPath = request.getRealPath(newFile);
File file = new File(newPath);
FileWriter fwriter = new FileWriter(file);
BufferWriter bw = new BufferWriter(fwriter);
bw.write("");
bw.newLine();
bw.close();
fwriter.close();
DownLoad:
String file = "**.*";
response.setCharacterEncoding("UTF-8");
file = java.net.URLEncoder.encode(file,"UTF-8");
response.setHeader("Content-Dispostion","attachment;filename="+file);
response.setContentType("application/*");
6 Other apply:
get request header content:
>>>
response.setContentType("text/html");
PriterWriter out = response.getWriter();
Enumeration e = request.getHeaderNames();
while( e.hasMoreElements() ){
String name = (String) e.nextElement();
String value = request.getHeader(name);
out.println(name + " = " + value + " = " + "<br>");
}
request method lists down:
getAttribute(String name)
setAttribute(String name,java.lang.Object obj)
removeAttribute(String name)
getAttributeNames() return Enumeration
getCookies() return Cookie[]
getCharacterEncoding()
getContentLength()
getMethod()
getRequestURI()
getProtocol()
getRemoteAddr()
getServerName()
getServerPort()
getServletPath()
getHeader(String name)
getHeaderNames() return Enumeration
getParameter(String name)
getParameterNames() return Enumeration
getParameterValues(String name)
response method lists down:
addCookie(Cookie cookie)
addHeader(String name,String value)
setHeader(String name,String value)
containHeader(String name)
flushBuffer()
getBufferSize()
getOutputStream()
sendError(int)
sendRedirect(String url)
setContentType(String contentType)
out method lists down:
clear()
clearBuffer()
close()
flush()
isAutoFlush()
print(String)
println(String)
session method lists down:
getAttribute(String name)
getAttributeNames()
removeAttribute(String name)
setAttribute(String name,java.lang.Object obj)
getCreationTimes()
getId()
getLastAccessedTime()
application method lists down:
getAttribute(String name)
getAttributeNames()
setAttribute(String name,java.lang.Object obj)
getInitParameter(String name)
getServletInfo()
pageContext method lists down:
getRequest()
getResponse()
getServletConfig()
getServletContext()
getSession()
setAttribute()
removeAttribute()
config method lists down:
getInitParameter(String name)
getInitParameterNames(String name)
getServerName()
getServletContext()
page method lists down:
getClass()
toString()
hashCode()
equals(Object o)
copy(Object o)
clone()
exception method lists down:
getMessage()
toString()
printStackTrace()
7 Cookie
public Cookie(String name,String value)
getName
setValue getValue
getMaxAge setMaxAge
getPath setPath
getDomain setDomain
getComment setComment
getVersion setVersion
getSecure setSecure
8 Session
session track:
1> use cookie
HttpSession session = request.getSession();
session.setMaxInactiveinterval(60*60*24);
......
2> rewrite URL
api:encodeURL , encodeRedirectURL
service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException :
RequestDispatcher sessionServlet = getServletContext().getRequestDispatcher("/SessionServlet");
out.println( response.encodeURL("SessionServlet") );
modify web.xml
9 Filter
interface : Filter FilterChain FilterConfig
api :
init(FilterConfig)
doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException
destroy()
apply :
FilterConfig config;
String filterthing;
init::
10 Listener
11 Other
Struts2
1 Struts2 is MVC framework running in J2EE, implemented by Servlet and JSP technology.
2 JSP + Servlet + JavaBean
3 download and install -- refer to net page
4 work chain :
FilterDispatcher
|
|
Action
|
|
Result
|
|
browserclient
5 develop chain :
web.xml --- JSP --- Action(java) --- struts.xml(mapping) --- testing...
Hibernate
data (such as a object in memory) save into HD,DB,or xml file persistently.
2 its performence
......
3 MyEclipse MySql
4 develop chain :
1> create db
2> hibernate.cfg.xml --> <property name="show_sql"> true </property>
3> create object classjava
4> create orm file
5> hibernate.cfg.xml --> <mapping resource="ormfile.xml"/>
5 testing :
Table :
CREATE TABLE student(
id varchar(10) NOT NULL,
name varchar(20) default NULL,
result double(3,1) default NULL,
subject varcharr(10) default NULL,
PRIMARY KEY(id)
)
hibernate.cfg.xml :
<property name="show_sql"> true </property>
Student.java :
package test;
public class Student {
private String id;
private String name;
private String subject;
private float result;
/* get/set Method */
}
Student.hbm.xml :
<hibernate-mapping schema="" catalog="" default-cascade="" default-access="" default-lazy="" auto-import="" package="">
<class name="test.Student" table="" mutable="" proxy="" lazy="true|false" dynamic-update="true|false" dynamic-insert="true|false" >
<id name="id" type="" colume="" unsaved-value="" access="" ><generator class="assigned"></generator></id>
<property name="name" type="" colume="" access="" not-null="" generated="never" ></property>
<property name="subject"></property>
<property name="result"></property>
</class>
</hibernate-mapping>
hibernate.cfg.xml :
<session-factory>
<mapping resource="Student.hbm.xml"/>
</session-factory>
6 apply :
TestInsert.java
Student stu = new Student();
stu.setXXX("");
Session session = HibernateSessionFactory.getSession();
Transaction ta = null;
try{
ta = session.beginTransaction();
session.save(stu);
ta.submit();
System.out.println("Succedd!");
}catch(Exception ex){
ex.printStackTrace();
ta.rollback();
}
HibernateSessionFactory.closeSession();
TestQuery.java
Session session = HibernateSessionFactory.getSession();
Query query = session.createQuery("from Student as stu where stu.result >= 85");
List list = query.list();
Iterator it = list.iterator();
Student stu = null;
System.out.println();
while(it.hasNext()){
stu = (Student) it.next();
System.out.println(stu.id + " " + stu.name + " ");
}
HibernateSessionFactory.closeSession();
7 Other
Annotations :
3-conditions : public class ; not abstract class ; public A()
import javax.persistence.Entity;
@Entity(name="Employee")
public class Employee {}
Using Example :
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.Column;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="tb_employee" , catalog="mysqltest")
public class Employee {
@Id
// @GeneratedValue(strategy="javax.persistence.GeneratedType.AUTO |IDENTITY |SEQUENTITY |TABLE")
// or
// @GenericGenerator(name="myuuid" strategy="uuid")
//@GeneratedValue(generator="myuuid")
private String employeeID;
@Column(name="employeeName")
private String employeeName;
public String getEmployeeID(){
return employeeID;
}
public void setEmployeeID(String eID){
this.employeeID = eID;
}
}
You must implement the AnnotationSessionFactory.java
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationsConfiguration;
private static String CONFIG_FILE = "/hibernate.cfg.xml";
private static AnnotationsConfiguration configuration = new AnnotationsConfiguration();
private static ThreadLocal<Session> threadlocal = new ThreadLocal<Session>();
private static org.hibernate.SessionFactory sessionFactory;
private static String config_file = CONFIG_FILE;
Session
pass SessionFactory to get Session object.
public Serializable save(Object object) throws HibernateException;
public Serializable save(String entityName, Object object) throws HibernateException;
public Object load(String class,Object object) throws HibernateException;
public void refresh(Object object) throws HibernateException;
public void refresh(Object object,LockMode mode) throws HibernateException;
public void delete(Object object) throws HibernateException;
public void delete(String entityName, Object object) throws HibernateException;
Spring
|
|
SSH--------- Hibernate(SQL)
|
| --- IoC BeanFactory-->XMLBeanFactory
------ Spring --|
--- AOP
1 Spring :
Data Access Layer isolated with Business Logic Layer.
2 Spring develop chain :
public interface XXXService {}
public XXXServiceImpl implements XXXService{}
applicationContext.xml :
<bean id="yourid" class="packagename.XXXServiceImpl" scope="singleton|prototype|request|session|global session"></bean>
or singleton="true|false" prototype="true|fasle"
testing :
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
XXXService service = (XXXService)factory.getBean("yourid");
......
3 IoC Container
Bean put into Container, it can be managed by Container.
BeanFactory : org.springframework.beans.factory.BeanFactory
3 ways to get Container object :
Resource rs = new FileSystemResource("/src/applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(rs);
ClassPathResource cprs = new ClassPathResource("applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(cprs);
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeanFactory bf = context;
4 Dependency injection
Setter and Contructor
Setter : <property name="field1"> <value>ID_Value</value> </property> insert into applicationContext.xml
Contructor : <contructor-arg index="0"><value>ID_Value_0</value></contructor-arg>
<contructor-arg index="1"><value>ID_Value_1</value></contructor-arg>
5 AOP
support : lib/aspectj/aspectjweaver.jar aspectjrt.jar
config the applicationContext.xml : add the label <aop:aspectj-autoproxy/>
using :
@Aspect
public class XXXServiceImpl{
private int Id;
@Poincut ("execution(***)") // within this target args || && !
private void toString(){
}
@Before("toString()") // @AfterThrowing @AfterReturning @After @Around
private void BeforeMethod(){
}
}
6 JDBC
import org.springframework.jdbc.core.JdbcTemplete;
import javax.sql.DataSource;
public class Operater {
private JdbcTemplete jt;
private DataSource ds;
}
create table :
jt = new JdbcTemplete(ds);
jt.execute("create table student(id integer,name varchar(100))");
update table :
jt = new JdbcTemplete(ds);
jt.update("insert into student(1,"zjm")"); // update insert delete
select table :
jt = new JdbcTemplete(ds);
Object object = jt.queryForObject("select name from student where id=1",Object);
List list = jt.queryForList();
Summary
Model :
Presentation Layer
Business Logic Layer
Data Access Layer --- DAO
Persistence Layer --- Hibernate
DB Layer --- MySql,DB2...