一、创建一个项目
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!");
}
}