The server encountered an unexpected condition that prevented it from fulfilling the request.

在servlet中的post请求中添加switch op 就产生了以下错误
图片说明

请大佬帮忙看下怎么解决

下方servlet代码
package com.fruit.tray.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

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

import com.alibaba.fastjson.JSON;
import com.fruit.tray.bean.User;
import com.fruit.tray.service.UserService;
import com.fruit.tray.service.impl.UserServiceImpl;

@WebServlet(urlPatterns = "/servlet/user")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private UserService userService = new UserServiceImpl(); 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// 避免请求意外的设置
response.setContentType("text/html;charset=utf-8");
/*设置响应头允许ajax跨域访问*/
response.setHeader("Access-Control-Allow-Origin", "*");

    /* 星号表示所有的异域请求都可以接受, */
    response.setHeader("Access-Control-Allow-Methods", "GET,POST");
    UserService userService = new UserServiceImpl();

    String op = request.getParameter("op");
    switch (op) {
    case "login":

         try {
            login(request, response, userService);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        break;
    case "regist":
        try {
            regist(request, response, userService);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        break;
    default:
        break;
    }




    //返回值给微信小程序
    /*Writer out = response.getWriter();
    out.write("进入后台了");

    out.flush();*/
}

private void regist(HttpServletRequest request, HttpServletResponse response, UserService userService)
        throws SQLException, IOException {
    String rphone = request.getParameter("phone");
    String rpassword1 = request.getParameter("pwd1");
    String rpassword2 = request.getParameter("pwd2");
    String rsex = request.getParameter("sex");
    String raddr1 = request.getParameter("addr1");
    String raddr2 = request.getParameter("addr2");
    String rthree = request.getParameter("three");
    User ruser = new User("null", rphone, rpassword1, rthree, raddr1, raddr2, 18, rsex, "null", "null", null, "null");


        User regist = userService.regist(ruser);


    if (ruser != null) {
        System.out.println("注册成功");
        PrintWriter write = response.getWriter();
        write.println("注册成功返回前台");
    }else {
        PrintWriter write = response.getWriter();
        write.println("注册失败返回前台");
    }
}

private void login(HttpServletRequest request, HttpServletResponse response, UserService userService)
        throws SQLException, IOException {
    //获取微信小程序get的参数值并打印
    String phone = request.getParameter("phone");
    String password = request.getParameter("pwd");
    System.out.println("phone:" + phone + "password:" + password);
    User user = null;

        user = userService.login(phone, password);

    if (user != null) {
        System.out.println(user.getUserName());

// 对象翻译成json字符串
String jsonObject = JSON.toJSONString(user);
System.out.println(jsonObject);

// list对象数组翻译成json字符串
List list = new ArrayList<>();
list.add(user);

        String jsonList = JSON.toJSONString(list);  
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("utf-8"); 
        System.out.println(jsonList);
        PrintWriter out = response.getWriter();
        out.println(jsonList);

    }else {
        PrintWriter write = response.getWriter();
        write.println("用户名不匹配");

    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐