java servlet 使用

1  创建  Test 包 并创建 Test.java

package Test;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Test extends HttpServlet
{
       /**
         *
         */
       private static final long serialVersionUID = 1L;
       protected void doGet(HttpServletRequest request,HttpServletResponse response) throws
       ServletException,IOException{
          PrintWriter out = response.getWriter();
          out.println("This is a servlet test");
//          out.println("<html><body><b3>'This is a servlet test.'</be></body></html>");
          out.flush();
      }

}


2 配置 web.XML文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>myApp</display-name>
  <description>
   A application for test.
  </description>
  <servlet>
    <servlet-name>Test</servlet-name>
    <servlet-class>Test.Test</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name> Test</servlet-name>
    <url-pattern>/Test</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


debug 运行 成功 进行第三步


3 创建iOS 项目 使用AFNetworking 进行请求测试

    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];

    NSString *str = @"test";
    
    NSDictionary *parameters = @{@"test" : str};
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain", @"text/json", @"text/javascript", @"text/html",nil];
    manager.requestSerializer.timeoutInterval =25;//请求超时时间
     NSString *url = @"http://192.168.0.102:8080/myApp/Test";
    [manager GET:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        NSLog(@"返回结果%@",responseObject);
        NSString *result  =[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
               NSLog(@"返回结果%@",result);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

 

  运行Xcode  报错误

 Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

  加入 代码
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    
    [securityPolicy setAllowInvalidCertificates:YES];
    [manager setSecurityPolicy:securityPolicy];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

 

再次运行 日志输出  “返回结果This is a servlet test”

 测试成功

 注意 如果servlet  返回结果如果是html 格式  不需要  加入 @"text/plain" 否则必须添加


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值