当前页面是查询页面:/EmpMangager/findEmp
FindEmpServlet.java
package Web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.jasper.tagplugins.jstl.core.Out;
import dao.EmpDao;
import dao.EmpDaoImpl;
import entity.emp;
public class FindEmpServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//1.接收参数(没有)
//2.处理业务(查询)
EmpDao dao =new EmpDaoImpl();//实例化dao
List list=dao.findAll();
//3.输出响应(表格)
res.setContentType("text/html;charset=utf-8");
PrintWriter out = res.getWriter();
out.println("增加");//超链接路径是目标路径的相对路径
out.println(" ");
out.println(" ");
out.println(" 编号 ");
out.println(" 姓名 ");
out.println(" 职位 ");
out.println(" 月薪 ");
out.println(" ");
if(list!=null){
for (emp e : list) {
out.println("");
out.println(" "+e.getEmpno()+" ");
out.println(" "+e.getEname()+" ");
out.println(" "+e.getJob()+" ");
out.println(" "+e.getSal()+" ");
out.println(" ");
}
}
out.println("
");
out.close();
}
}
web.xml
findEmp
Web.FindEmpServlet
findEmp
/findEmp
目标是增加页面:/EmpManager/add_emp.html
相对路径是:add_emp.html
add_emp.html
Insert title here
点击保存后调用访问/addEmp
AddEmpServlet.java
package Web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.jasper.tagplugins.jstl.core.Out;
import dao.EmpDao;
import dao.EmpDaoImpl;
import entity.emp;
public class AddEmpServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//1.接收参数
req.setCharacterEncoding("utf-8");//处理POST方式中文乱码
String ename = req.getParameter("ename");
String job = req.getParameter("job");
String sal= req.getParameter("sal");
//2.处理业务
emp e = new emp();
e.setEname(ename);
e.setJob(job);
if(sal!=null&&!sal.equals("")){
e.setSal(new Double(sal));//自动装箱
}
EmpDao dao=new EmpDaoImpl();
dao.save(e);
3.输出响应
res.setContentType("text/html;charset=utf-8");
PrintWriter out =res.getWriter();
out.println("保存成功
");
out.close();
}
}
保存成功后重定向到查询页面
//重定向到查询页面,就是建议浏览器访问查询页面
//当前路径:/EmpManager/addEmp
//目标路径:/EmpMaager/findEmp
res.sendRedirect("findEmp");入代码片
建议: 将访问路径理解为一个资源的名字
从表面上看URI包含URL
URI包含URL
适合小规模项目
适合一个项目只写一个Servlet
适合一个项目写少量的几个Servlet
login
web.LoginServlet
//设置参数maxOnline
maxOnline
3000
login
/login
LoginServlet.java
package web;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
/*Tomcat创建servlet逻辑:
* LoginServlet s= new LoginServlet();
* ServletConfig c= new ServletConfig
* c.加载数据();从web.xml中加载数据
* s.init(c)*/
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
String maxOnline = config.getInitParameter("maxOnline");//读取设定的参数
System.out.println(maxOnline);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
ServletConfig cfg=getServletConfig();//init()传入的那个config
String maxOnline=cfg.getInitParameter("maxOnline");
System.out.println(maxOnline);
System.out.println("正在登录...");
}
}
size
20
FindEmpServlet.java
package web;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FindEmpServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
/*
* tomcat启动时就会创建唯一的context,并且会调用它的方法加载web.xml中的公用参数
* context是全局的,任何servlet都可以使用
*/
ServletContext ctx = getServletContext();
String size = ctx.getInitParameter("size");
System.out.println(size);//size在多个servlet中都要使用
System.out.println("分页查询员工数据");
}
}
FindDeptServlet.java
package web;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FindDeptServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
/*tomcat启动时就会创建唯一的context,并且会调用它的方法加载web.xml中的公用参数
* context是全局的,任何servlet都可以使用
* */
ServletContext ctx = getServletContext();
String size = ctx.getInitParameter("size");
System.out.println(size);
System.out.println("分页查询部门数据");
}
}
package web;
import java.util.concurrent.CountDownLatch;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class InitServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
//tomcat启动时会优先创建context然后再创建servlet
ServletContext context=getServletContext();
context.setAttribute("count", 0);
}
/*该Servlet不负责处理具体的业务,只用来在tomcat启动时初始化数据。
* 一般web项目都有1-2个这样的servlet*/
}
读取变量
Integer count = (Integer) ctx.getAttribute("count");
ctx.setAttribute("count", ++count);
System.out.println(count);
此时没有线程问题
此时存在线程安全问题
package web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UpServlet extends HttpServlet {
double salary = 2000.0;
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
synchronized (this) {
//模拟涨薪
salary+=100;
//网络延迟
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//显示数据
res.setContentType("text/html;charset=utf-8");
PrintWriter out=res.getWriter();
out.println(salary);
out.close();
}
}
}