jdbc
jdbc代码
import java.sql.*;
依然是关于丑陋的字符串常量,对于一些常量可以这么写:
public class Main {
public static void main(String[] args) {
String driverClassName = null;
Class.forName(driverClassName):
}
}
链接池
性能太差了。数据库直连每次要花100多ms来建立和关闭链接。
于是使用一个链接池,在链接用完之后可以设定为空闲。
import javax.sql.DataSource;
import javax.servlet.*;
public class AServlet extends {
public static void main(String[] args) {
DataSource ds = (DataSource) ctx.lookup("jdbc/TestDB");
getServletContext().setAttribute("ds", ds);
}
}
i18n
internationalization
java.util.Locale
java.util.ResourceBundle bundle = new ResourceBundle("res", )
bundle.getString("button.Add");
res.properties
res_en.properties
res_jp.properties
res_zh_CN.properties
<input type="submit" value="add">
button.add=Add
JNDI
java命名服务, java naming and directory interface
NamingContext
Context ctx =
Cart c = (Cart) ctx.lookup("/abc/xyz/cart/");
Context c1 = (Context) ctx.lookup("/abc"); // forms the
Cart c2 = (Cart) c1.lookup("/xyz/cart/);
assert c1 == c2;
lookup(String s);
bind(String name, Object o);
rebind(String name, Object o);
MVC
Controller没有可移植性,而V和M都有
为了提高它的可复用性,“降低程序的密度就可以提高可复用性”
Struts 1.
是一个MVC框架。
提供了一大堆类,FrontEndController & BackEndController. 前端控制器由ActionServlet
充当。
ActionServlet.execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
}
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</utl-pattern>
</servlet-mapping>
<action
path="/InputSubmit"
type="app.InputSubmit"/>
考试题
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.?"
maxTotal="100" maxIdle="30" maxWaitMillis = "10"
username="javauser" password="javadude" driverClassName="com.mysql"/>
</Context>
- Struts
回去自己学的
- 设计模式
- JNDI
- InitialContext(Hashtable);
- ActionServlet
- DispatchAction