重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
ava中servlet类,首先建一个包cn.java.servlet,同样建一个cn.java.entity的包用来建立实体类。
本节是Java-web阶段第52课时,通过继承servlet类实现了,从浏览器端发送请求到服务器(这时候在console输出了hello servlet),然后通过输出流向外输出红色字体(hello servlet))。这时候从浏览器发送请求用的是doGet方法。HttpServletRequest和HttpServletRequest可以看成是特殊的实体类,即平时所看见的request方法和response方法。
package cn.java.Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
@WebServlet("/aaa")
public class HomeServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("hello servlet");
String str="
ServletOutputStream so=response.getOutputStream();
so.write(str.getBytes("utf-8"));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
}