实现servlet 接口(创建的类或是继承的父类一定要实现servlet接口)
public class QuickStratServlet implements Servlet{
@Override
public void init(ServletConfig config) throws ServletException {
//1、获得servlet的name----<servlet-name>abc</servlet-name>写在配置文件中web.xml中
String servletName = config.getServletName();
System.out.println(servletName);//abc
//2、获得该servlet的初始化的参数
String initParameter = config.getInitParameter("url");
System.out.println(initParameter);
//3、获得Servletcontext对象
ServletContext servletContext = config.getServletContext();
System.out.println("init running....");
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("QuickStratServlet running....");
res.getWriter().write("QuickStratServlet running....");
}
@Override
public void destroy() {
System.out.println("destroy running....");
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
}
继承HttpServlet
package com.itheima.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class QuickStartServlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("hello haohao...");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Servletcontext域对象
package com.itheima.context;
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 ContextServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获得ServletContext对象 这个是整个项目的对象可以对项目中文件进行操作
ServletContext context = this.getServletContext();//this指的的servlet吗?
//1、获得初始化参数
String initParameter = context.getInitParameter("driver");
System.out.println(initParameter);
//2、获得a b c d.txt的绝对路径
//2.1 获得a.txt
String realPath_A = context.getRealPath("a.txt");
System.out.println(realPath_A);
//2.2 获得b.txt
String realPath_B = context.getRealPath("WEB-INF/b.txt");
System.out.println(realPath_B);
//2.3 获得c.txt
String realPath_C = context.getRealPath("WEB-INF/classes/c.txt");
System.out.println(realPath_C);
//2.4 获得d.txt----获取不到
//在读取src(classes) 下的资源是可以同类加载器----专门加载classes 下的文件的
//getResource() 参数是一个相对地址 相对classes
String path = ContextServlet.class.getClassLoader().getResource("c.txt").getPath();
System.out.println(path);
//3、域对象---向servletContext中存数据
context.setAttribute("name", "zhangsan");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
综合案例:
package com.itheima.login;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.itheima.domain.User;
import com.itheima.utils.DataSourceUtils;
public class LoginServlet extends HttpServlet {
@Override
public void init() throws ServletException { 服务器开启的时候才会运行这个函数, 用于初始化数据
//在Seveltcontext域中存一个数据count
int count = 0;
this.getServletContext().setAttribute("count", count);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {//每次访问都会执行一次
//username=zhangsan&password=123
//1、获得用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
//2、从数据库中验证该用户名和密码是否正确
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from user04 where uname=? and upassword=?";
User user = null;
try {
user = runner.query(sql, new BeanHandler<User>(User.class), username,password);
} catch (SQLException e) {
e.printStackTrace();
}
//3、根据返回的结果给用户不同显示信息
if(user!=null){
//从servletcontext中取出count进行++运算
ServletContext context = this.getServletContext();
Integer count = (Integer) context.getAttribute("count");
count++;
//用户登录成功
response.getWriter().write(user.toString()+"---you are success login person :"+count);
context.setAttribute("count", count);
}else{
//用户登录失败
response.getWriter().write("sorry your username or password is wrong");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Login.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WEB13/login" method="post">// /项目名+URL
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<input type="submit" value="登录"><br/>//用户按提交的时候才会跳转action
</form>
</body>
</html>
总结:
1.String username = request.getParameter("username");
//键值都为String类型(传入参数和返回结果为String)
String password =request.getParameter("password");
//键值都为String类型
//request.getAttribute(arg0)参数为String类型,返回的是Object类型(域对象中的键为String,值为object类型)
2. /服务端的路径和客户单的路径的区别,服务端的额路径不用加项目名(url),而客户端的路径需要加项目名(项目名加url )
3. / <init-param>
//<param-name>url</param-name>
//<param-value>jdbc:mysql:///mydb</param-value>
//</init-param>
//其实没什么实际的意义就是初始化servlet的时候通过初始化的一些参数(通过调用getinitparameter()方法)
4.报404的原因可能是地址有问题