IntelliJ IDEA 2017 Servlet的配置


以前在MyEclipse中写了Servlet的话需要在web.xml中写匹配

但是在IntelliJ IDEA中是略微有些不一样的,采用了一种更便捷的方法

拿刚刚写的一个小Demo举例吧


JSP页面代码:


<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title


通讯录

用户名:

密码:


Servlet代码

package com.limbo.Servlet;

import com.limbo.Bean.Contacts;
import com.limbo.Bean.Group;
import com.limbo.Bean.Page;
import com.limbo.Bean.Users;
import com.limbo.Dao.ContactDao;
import com.limbo.Dao.ImplDao.ContactBookImplDao;

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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "Servlet",urlPatterns = "/Servlet") //这一段就是用于匹配的代码,urlPatterns为Servlet的地址
public class Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        String method = request.getParameter("method");
        if (method!=null&&!"".equals(method)){
            if("Show".equals(method))
            {
                Show(request,response);
            }else if("Login".equals(method))
            {
               Login(request,response);
            }
            else if ("Add".equals(method)) {
               Add(request, response);
            }
        }
    }
    protected void Show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ContactDao contactDao=new ContactBookImplDao();
        Contacts contacts=new Contacts();

        String currpage=request.getParameter("currPage");
        if (currpage == null) {
            currpage = "0";
        }
        Page page=contactDao.showAllContacts(contacts,Integer.parseInt(currpage));
        HttpSession session = request.getSession();
        session.setAttribute("contacts_list",page);
        response.sendRedirect("Show.jsp");

    }
    protected void Login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ContactDao contactDao=new ContactBookImplDao();
        Users users=contactDao.login(request.getParameter("Loginname"),request.getParameter("password"));
        HttpSession session=request.getSession();
        session.setAttribute("users_order",users.getUsers_order());
        if (users!=null){
            response.sendRedirect("/Servlet?method=Show");
        }else {
            response.sendRedirect("error.jsp");
        }

    }
    protected void Add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
        Group group=new Group();
        Contacts contacts=new Contacts();
        ContactDao contactDao=new ContactBookImplDao();
        HttpSession session=request.getSession();


        contacts.setContacts_group(request.getParameter("GroupId")); //组别
        contacts.setContacts_name(request.getParameter("Contacts_Name"));//姓名
        contacts.setContacts_sex(request.getParameter("Contacts_Sex"));//性别
        contacts.setContacts_tel(Integer.parseInt(request.getParameter("Contacts_Tel")));//电话
        contacts.setUsers_order((Integer) session.getAttribute("users_order")); //用户ID
        String getBirth=request.getParameter("Contacts_Birth");//生日

        Date Contacts_Birth= null;
        try {
            Contacts_Birth = fmt.parse(getBirth);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        contacts.setContacts_birth(Contacts_Birth);

        contactDao.addContacts(contacts);
        response.sendRedirect("/Servlet?method=Show");

    }

}


你可能感兴趣的:(IntelliJ,IDEA)