javaWeb使用servlet搭建服务器入门-成都快上网建站

javaWeb使用servlet搭建服务器入门

百度百科说法:

成都创新互联专注于绩溪网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供绩溪营销型网站建设,绩溪网站制作、绩溪网页设计、绩溪网站官网定制、微信小程序定制开发服务,打造绩溪网络公司原创品牌,更为您提供绩溪网站排名全网营销落地服务。

Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态Web内容。

通俗讲法:

是运行在服务器端的一小段Java程序,接受和响应从客户端发送的请求

作用:

处理客户端请求,并且对请求做出响应

编写一个serclet步骤

1、编写一个类

继承自HttpServlet

重写doGet和doPost方法

2、编写配置文件(web.xml)

先注册后绑定

3、访问

http://localhost/项目名/路径

注意:

接收参数: 格式:value=key

String  value = request.getParameter("key");

例如:http://localhost/day09/hello?username=tom

中,String value = request.getParameter("username");

回写参数:

response.getWriter().print("success");

处理响应中的乱码问题:

resp.setContentType("text/html;charset=utf-8");一般放在第一行

以下是原码:

public class RequestServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
     resp.setContentType("text/html;charset=utf-8");
   // 接收参数
    String value = req.getParameter("username");
    System.out.println(value);
    //向浏览器回写数据
    resp.getWriter().print("data:"+value);
    
    resp.getWriter().print("你好");
  }
}

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>

 
 
   HelloServlet
   cn.itcast.a_hello.HelloServlet
 
 
   RequestServlet
   cn.itcast.b_request.RequestServlet
 
 
 
   HelloServlet
   /hello
 
 
   RequestServlet
   /request
 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


新闻名称:javaWeb使用servlet搭建服务器入门
文章地址:http://kswjz.com/article/iedpch.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流