苦瓜不要加糖 2021-06-03 08:58 采纳率: 33.3%
浏览 104
已采纳

eclipse运行servlet报错,运行整个web项目不报错

import java.io.DataOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MyServlet
 */
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		super.destroy();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		String resname = request.getParameter("resname");
		String remark = request.getParameter("remark");
		//对中文乱码进行转码  浏览器乱码用GBK app客户端用UTF-8
		String rname = new String(name.getBytes("ISO-8859-1"),"GBK");
		String rpwd = new String(pwd.getBytes("ISO-8859-1"),"GBK");
		//String rresname = new String(resname.getBytes("ISO-8859-1"),"GBK");
		//String rremark = new String(remark.getBytes("ISO-8859-1"),"GBK");
		String result = "F";// T-->注册成功   F-->注册失败
		//把用户信息写入MySQL数据库
		User users = new User();
		users.setName(rname);
		users.setPwd(rpwd);
		users.setResname(resname);
		users.setRemark(remark);
		MyDB mydb = new MyDB();
		if(mydb.AddUser(users)) {
			result = "T";
		}
		
		//如果是登陆,使用select语句,对返回T,错返回F
		System.out.println("用户名:"+rname+"  密码:"+rpwd);
		DataOutputStream dos = new DataOutputStream(response.getOutputStream());
		dos.writeUTF(result);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		doGet(request, response);
	}

}

  • 写回答

4条回答 默认 最新

  • 小P聊技术 2021-06-03 09:06
    关注

    你的这两行有错误 你打印下看看 name的值,看看是不是null,空指针了。

    //对中文乱码进行转码  浏览器乱码用GBK app客户端用UTF-8
    		String rname = new String(name.getBytes("ISO-8859-1"),"GBK");
    		String rpwd = new String(pwd.getBytes("ISO-8859-1"),"GBK");
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 有段代码不知道怎么理解,const isToken = (config.headers || {}).isToken === false
  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00