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

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条)

报告相同问题?

悬赏问题

  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?