我在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)
答案 0 :(得分:1)
您需要在SaveName.jsp
中导入UserData
类
将它添加到SaveName.jsp
中的jsp代码的顶部<%@ page import="javabeans.UserData" %>
答案 1 :(得分:0)
我不确定你想在这里实现什么,但我可以简单介绍一下jsp servlets
首先你可以创建一个简单的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>