请教大佬,我这段代码哪里出错了?

从表单获取数据出错
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页</title>
</head>
<body>
<form action="/Text/add" method="psot">
    <table>
        <td>
            用户id:<input name="userId" value="" type="text">
        </td>
        <td>
            用户名:<input name="userName" value="" type="text">
        </td>
        <td>
            密码:<input name="userPassword" value="" type="text">
        </td>
        <td>
            <input value="提交" type="submit">
        </td>
    </table>
</form>
</body>
</html>

servlet层

package com.text.servlet.user;

import java.io.IOException;
import java.io.PrintWriter;

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.text.entity.user;

/**
* Servlet implementation class add
* 
 */
@WebServlet("/Text/add")
public class add extends HttpServlet {
private static final long serialVersionUID = 1L;
   

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    
    doGet(request, response);
    //设置字符集
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=utf-8");
    
    String userId = request.getParameter("userId");
    String username = request.getParameter("userName");
    String psw = request.getParameter("userPassword");
    
    //创建用户实体类
    user u = new user(userId, username, psw);
    
    //连接数据库
    
    System.out.print(u);
    //重定向;
    if(true) {
        
        response.sendRedirect("/index.jsp");
    }else {
        PrintWriter out = response.getWriter();
        
        out.write("<script>");
        out.write("alert('用户注册失败')");
        out.write("location.href='insert.jsp'");
        out.write("<script>");
        
        }
    }
}

错误页面:
图片.png
项目目录:
图片.png

阅读 1.8k
3 个回答

是不是index.jsp里面的method写错了,你写成了psot

<form action="/Text/add" method="psot">

method="post"

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题