一、准备
1、首先在src目录下建一个与servlet有关的包
如下图:
新建的包应该是空的,图内不是新建的供参考。
然后右击包。。
然后起个名,注意名字的格式,点击finish即可。
2、如果想正常使用servlet需要有web.xml的配合
web.xml的位置。。
工程下的webcontent中的web-inf下,如果没有不要慌。自己建一个
new----》other
直接点next
起个名web.xml
新建完是这样↑
空空如也,没有代码自己添加↓
<?xml version="1.0" encoding="UTF-8"?>
<web-app >
<display-name>jspdemo2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.weihai.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
PS:1.注意格式,这个的格式好像特别严。。(个人感觉)
2.servlet标签的作用:告诉服务器声明一个servlet组件
3.servlet-mapping标签的作用:定义映射的URL地址
二、然后就可以在servlet中编写代码了。
以下是一个购物网站增、删、改、查用户的servlet代码
package com.weihai.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.weihai.bean.User;
import com.weihai.dao.UserDao;
import com.weihai.factory.DaoFactory;
/**
* Servlet implementation class User
*/
@WebServlet("/User")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UserServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String operate=request.getParameter("operate");
UserDao userDao=DaoFactory.getUserDao();
//查询所有记录
if("queryAll".equals(operate)) {
ArrayList<User> users=userDao.queryAll();
//把数据users封装到request对象
request.setAttribute("users", users);
request.getRequestDispatcher("userList.jsp").forward(request, response);
}
//删除记录
if("delete".equals(operate)) {
String tId=request.getParameter("id");
int id=0;
if(tId!=null&&!tId.isEmpty()) {
id=Integer.parseInt(tId);
}
boolean t=userDao.delete(id);
if(t) {
response.sendRedirect("UserServlet?operate=queryAll");
}else {
response.sendRedirect("fail.jsp");
}
}
//添加记录
if("insert".equals(operate)) {
// 1.获取用户输入的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 2.把数据封装到user对象
User user = new User();
user.setUsername(username);
user.setPassword(password);
boolean t=userDao.insert(user);
if(t) {
response.sendRedirect("UserServlet?operate=queryAll");
}else {
response.sendRedirect("fail.jsp");
}
}
//按照ID查找用户
if("queryById".equals(operate)) {
//获取用户ID
String tId=request.getParameter("id");
int id=0;
if(tId!=null&&!tId.isEmpty()) {
id=Integer.parseInt(tId);
}
//按照ID查询用户信息
User user= userDao.queryById(id);
//把数据User封装到request对象
request.setAttribute("user", user);
request.getRequestDispatcher("userupdate.jsp").forward(request, response);
}
if("update".equals(operate)) {
// 1.获取用户输入的用户名和密码
String tId=request.getParameter("id");
String username = request.getParameter("username");
String password = request.getParameter("password");
// 2.把数据封装到user对象
int id = 0;
if(tId!=null&&!tId.isEmpty()) {
id=Integer.parseInt(tId);
}
User user = new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
boolean t=userDao.update(user);
if(t) {
response.sendRedirect("UserServlet?operate=queryAll");
}else {
response.sendRedirect("fail.jsp");
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
以下是我src文件夹下的内容。