0

我在jsp中编写了一个hello world程序,现在我正在尝试通过JSP处理表单。

我的 jsp 表单(GetName.jsp)看起来像这样

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>


<FORM METHOD=POST ACTION="SaveName.jsp">
Name <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
Email <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
Age <INPUT TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
    </body>
</html>

同样,SaveName.jsp 看起来像这样

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <jsp:useBean id="userData" class="javabeans.UserData" scope="session"/>
<jsp:setProperty name="userData" property="*"/> 


</BODY>
</HTML>
    </body>
</html>

在名为 javabeans 的包中的同一个项目中,名为 UserData 的类看起来像这样。

package javabeans;

public class UserData {

    String username;
    String email;
    int age;

    public void setUsername( String value )
    {
        username = value;
    }

    public void setEmail( String value )
    {
        email = value;
    }

    public void setAge( int value )
    {
        age = value;
    }

    public String getUsername() { return username; }

    public String getEmail() { return email; }

    public int getAge() { return age; }

}

现在当运行 GetName.jsp 我得到以下错误

D:\javaworkspace\Netbeans7-2\HelloWeb\build\generated\src\org\apache\jsp\SaveName_jsp.java:56: cannot find symbol
symbol  : class UserData
location: class org.apache.jsp.SaveName_jsp
      UserData user = null;
      ^
D:\javaworkspace\Netbeans7-2\HelloWeb\build\generated\src\org\apache\jsp\SaveName_jsp.java:58: cannot find symbol
symbol  : class UserData
location: class org.apache.jsp.SaveName_jsp
        user = (UserData) _jspx_page_context.getAttribute("user", PageContext.SESSION_SCOPE);
D:\javaworkspace\Netbeans7-2\HelloWeb\build\generated\src\org\apache\jsp\SaveName_jsp.java:60: cannot find symbol
symbol  : class UserData
location: class org.apache.jsp.SaveName_jsp
          user = new UserData();
3 errors
D:\javaworkspace\Netbeans7-2\HelloWeb\nbproject\build-impl.xml:930: The following error occurred while executing this line:
D:\javaworkspace\Netbeans7-2\HelloWeb\nbproject\build-impl.xml:284: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 2 seconds)
4

2 回答 2

1

您需要UserData在 SaveName.jsp 中导入类

将此添加到 SaveName.jsp 中 jsp 代码的顶部

<%@ page import="javabeans.UserData" %>
于 2012-11-20T13:41:21.670 回答
0

我不确定你想在这里实现什么,但我可以给你关于 jsp servlet 的简单想法

首先你可以创建一个简单的jsp程序。它将询问用户姓名和电子邮件 ID,并在表单操作中将其重定向到 abc servlet。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
    <title> My first JSP   </title>
</head> 
<body>      
    <form action="abc">         
         Please enter a name <br>
        <input type="text" name="name"size="20px">
                     Please enter an email <br>
        <input type="text" name="email"size="20px">
        <input type="submit"     value="submit">                        
    </form>     
</body> 
</html>

然后创建“abc” servlet 将此代码放在您的 servlet 中。它将从 jsp 页面获取值并显示它。

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class abc extends HttpServlet { 
protected void doGet(HttpServletRequest request, 
  HttpServletResponse response) throws ServletException, IOException 
  {
// reading the user input
String name= request.getParameter("name");    
String email= request.getParameter("email");    
String msg="I'm"+name+"id is"+email;
PrintWriter out = response.getWriter();
out.println (
  "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" 
      \"http://www.w3.org/TR/html4/loose.dtd\">\n" +
  "<html> \n" +
    "<head> \n" +
      "<meta http-equiv=\"Content-Type\" content=\"text/html; 
        charset=ISO-8859-1\"> \n" +
      "<title> Hi </title> \n" +
    "</head> \n" +
    "<body> \n" +
    msg +
      "</font> \n" +
    "</body> \n" +
  "</html>" 
        );  
   }  
 }

在 "web.xml" 中定义您的 servlet。您需要在 web.xml 文件中进行 servlet 映射。

   <servlet>
<servlet-name>abc</servlet-name>
<servlet-class>abc</servlet-class>
   </servlet>
   <servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/abc</url-pattern>
  </servlet-mapping>
于 2012-11-20T14:29:49.280 回答