📄 servlet_day02.txt
字号:
1. public class MyServlet extends HttpServlet {
public void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException {
}
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException {
}
}
2. Web应用名称
|
-- WEB-INF
|
-- register.html
|
-- web.xml
|
-- lib
|
-- classes
|
-- ch01
|
--- DateServlet.class
3. request.getParameter(""); // String
request.getParameterValues(""); //String[]
request.getParameterNames(""); //Enumeration
request.getQueryString(); //String
http://localhost:8080/a/register?name=zs&age=10
web.xml
<servlet>
<servlet-name></servlet-name>
<servlet-class></servlet-class>
<init-param>
<param-name>age</param-name>
<param-value>10</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name></servlet-name>
<url-pattern>/a</url-pattern>
</servlet-mapping>
url-pattern内容形式:
1. 固定路径:
/a, /b, /a/b
注意:一定要以 / 开头
2. 通配路径
a. /a/*, /*
b. *.do <-> ch02.MyServlet
*.action <-> ch02.MyServlet2
*.jd0801 <-> ch02.MyServlet3
<url-pattern>/c/*.do</url-pattern>
http://localhost:8080/应用名称/c/b.do
http://localhost:8080/应用名称/c.jd0801
WebRoot
|
-- a
|
--- register.html
http://localhost:8080/应用名称/a/register.html
public abstract class HttpServlet
extends GenericServlet {
ServletConfig config;
public void init(ServletConfig conf)
throws ServletException {
config=conf;
init();
}
public String getInitParameter(String name) {
return config.getInitParameter(name);
}
public void init() {}
public void log(String msg) {
getServletContext().log(msg);
}
}
ch03
InitServlet.java
/init
网页计数器:
您是第 3 位访客!!!
ch03
CounterServlet.java
/counter
http://localhost:8080/servlet_jd0801/counter
使用JDBC和数据库交互的步骤:
1. 注册Driver
*1) Class.forName(""); -> classes12.jar
2) Driver driver=new OracleDriver();
DriverManager.registerDriver(driver);
3) java -Djdbc.drivers=
2. 创建Connection
*1) DriverManager.getConnection(
String url,
String user,
String password);
2) Driver driver=new OracleDriver();
driver.connect(
String url,
Properties pro);
3. 实例化Statement
1) 简单Statement
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
int i=stmt.executeUpdate(sql);
boolean f=stmt.execute(sql);
2) PreparedStatement
PreparedStatement pstmt=
conn.prepareStatement(sql);
pstmt.setInt(1,1);
pstmt.setString(2,"a");
pstmt.setDate(3,new Date());
ResultSet rs=pstmt.executeQuery();
int i=pstmt.executeUpdate();
boolean f=pstmt.execute();
3) CallableStatement
String sql="{call procedure_name(?,?)}";
String sql="{?=call function_name(?,?)}";
CallableStatement cstmt=
conn.prepareCall(sql);
ResultSet rs=pcstmt.executeQuery();
int i=cstmt.executeUpdate();
boolean f=cstmt.execute();
4. 执行SQL
5. 处理结果集
select id,name
while(rs.next()) {
rs.getString("name");
}
6. 关闭对象,释放资源
rs.close();
stmt.close();
conn.close();
当前工程
|
---src
|
---WebRoot
|
-- WEB-INF
|
-- lib
|
-- classes12.jar
$CATALINA_HOME/common/lib/classes12.jar
ch04
DBServlet.java
/db
http://localhost:8080/servlet_jd0801/db
ID LAST_NAME SALARY
1 zs 1000
2 ls 2000
JNDI使用
1. 获得上下文
Context ctx=new InitialContext();
Properties pro=new Properties();
Context ctx=new InitialContext(pro);
2. 绑定
ctx.bind("JNDI名字", new OracleDataSource());
3. 查找
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("JNDI名字");
Connection conn=ds.getConnection();
使用Tomcat配置数据源步骤:
1. 将数据库驱动程序置于目录:
$CATALINA_HOME/common/lib
2. 使用Tomcat的Admin应用以图形化方式配置数据源
$CATALINA_HOME/conf/Catalina/localhost/servlet_jd0801.xml
3. 修改web.xml文件,加入以下信息:
<resource-ref>
<res-ref-name>jdbc/oracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
4. 使用代码获得数据源:
Context ctx=new InitialContext();
DataSource ds=(DataSource)
ctx.lookup("java:comp/env/jdbc/oracle");
Connection conn=ds.getConnection();
ch04
DataSourceServlet
public class HttpServletRequestImpl implements
HttpServletRequest {
private Map<String, Object>
map=new HashMap<String,Object>();
public void setAttribute(
String key,
Object value) {
map.put(key,value);
}
public Object getAttribute(
String key) {
return map.get(key);
}
}
getServletContext().getRequestDispatcher(“/students.jsp”).forward(req, res);
RequestDispatcher dispatcher=getServletContext().getRequestDispatcher(“/students.jsp”)
dispatcher.forward(req,res);
ch04 ch04 ch04
ForwardServlet ResultServlet IncludeServlet
/forward /result /include
public class RegisterServlet extends HttpServlet {
public void doGet(...) {
String name=request.getParameter("name");
...
Customer customer=new Customer();
customer.setName(name);
...
CustomerDAO dao=
new CustomerDAOImpl();
try {
dao.save(customer);
RequestDispatcher dis=
getServletContext().
getRequestDispatcher("/login.html");
dis.forward(request,response);
} catch(Exception e) {
RequestDispatcher dis=
getServletContext().
getRequestDispatcher("/register.html");
dis.forward(request,response);
}
}
}
public class LoginServlet extends HttpServlet {
public void doGet(...) {
String name=request.getParameter("name");
...
Customer customer=new Customer();
customer.setName(name);
...
CustomerDAO dao=
new CustomerDAOImpl();
try {
Customer c=dao.find(customer);
if(c==null)
return new Exception("");
RequestDispatcher dis=
getServletContext().
getRequestDispatcher("/list.html");
dis.forward(request,response);
} catch(Exception e) {
RequestDispatcher dis=
getServletContext().
getRequestDispatcher("/login.html");
dis.forward(request,response);
}
}
}
Log4J
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -