IntelliJ IDEA 搭建JSP+Servlet的web开发多个(八)

创建一个项目
File -> New Project -> 输入项目名称“myjsps” ,选择Project SDK为1.8

二、创建一个Module
鼠标点中项目名称demos -> 右键选择New,选择Module 

单击Module如图所示:


ModuleName  lesson3


创建src|New|servlet


ServletName: TestServlet


创建完后,发现是红色的,说明没有导入Apache的包


按ctrl+shift+Alt+S 打开Project Structure |选中Source|创建文件夹classes,lib


在选择Dependencies依赖包


单击JARs or directories


 单击完成后!就不会出现红色的哦!


启动部署:

:

创建了三个web(lesson1,lesson2,lesson3)


运行页面

lesson3工程运行效果



Servlet

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 18-5-15.
 */
@javax.servlet.annotation.WebServlet(name = "TestServlet",urlPatterns = {"/tt"})
public class TestServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        response.setContentType("text/html;charaset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("this is Servlet Annotation!");
    }
}

<a href="/tt">Click ME TT</a>


lesson1工程运行效果

 

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         PrintWriter out=response.getWriter(); //写入对象
         response.setContentType("text/html;charset=utf-8");  //设置响应的字符集格式为UTF-8
         request.setCharacterEncoding("utf-8");
         String name=request.getParameter("uname");
         String pass=request.getParameter("upass");

         out.println(name+"\t"+pass);

          response.sendRedirect("my");

    }

 

@WebServlet(name = "MyServlet")
public class MyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //必须写这个内容类项
        response.setContentType("text/html;charset=utf-8");

        //输出对象
        PrintWriter out=response.getWriter();
        out.println("<html><head><body><a href='reg'>click Me@@</a></body></head></html>");

       // response.sendRedirect("reg");

        //最后要记得清空缓存区,并且关闭。
        out.flush();
        out.close();
    }
}


@WebServlet(name = "RegServlet",urlPatterns = {"/reg"})
public class RegServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Test RegServlet Annotation!");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值