qq_44181883
苦瓜不要加糖
2021-06-03 08:58
采纳率: 100%
浏览 32

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条回答 默认 最新

  • qq_15769939
    小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");
    点赞 评论
  • software7503

    String name = request.getParameter("name");

    String pwd = request.getParameter("pwd");

    name,pwd有空值,确认一下登录界面的空间名称是否一致。

    点赞 2 评论
  • technologist_32
    CSDN专家-Time 2021-06-03 09:07

    传递了空指针 断点看一下doGet方法的空指针

    点赞 1 评论
  • QA_Assistant
    有问必答小助手 2021-06-03 19:04

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论

相关推荐