view
Example View
Example jsp view
The value added to the request was<%=request.getAttribute("date")%>
HiddenFieldServlet
model
public interface Service {
public void execute(HttpServletRequest request, HttpServletResponse response, ServletContext context)
throws Exception;
}
public class ExampleService implements Service {
public ExampleService() {
}
public void execute(HttpServletRequest request, HttpServletResponse response, ServletContext context)
throws Exception {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
request.setAttribute("date", "format.format(date)");
PrintWriter out = response.getWriter();
out.write("format.format(date)");
}
}
controller
public class Controller extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
protected void forward(HttpServletRequest request, HttpServletResponse response, String target)
throws ServletException {
try {
ServletContext context=getServletContext();
if (!target.equals("false")) {
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
dispatcher.forward(request, response);
}
} catch (IOException ioe) {
throw new ServletException(ioe.getMessage());
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String serviceName = request.getParameter("service");
if (serviceName == null) {
throw new ServletException("No service named!");
}
String target = request.getParameter("target");
if (target == null) {
throw new ServletException("No target named!");
}
ServletContext context = getServletContext();
try {
Class cls = Class.forName(serviceName);
Service service = (Service) cls.newInstance();
service.execute(request, response, context);
} catch (ClassNotFoundException ce) {
} catch (IllegalAccessException iae) {
throw new ServletException(iae.getMessage());
} catch (Exception e) {
throw new ServletException(e.getMessage());
}
forward(request,response,target);
}
}
在这个MVC中,找不到ExampleService类的原因是什么,为什么ExampleService里的数据无法传达给ExampleView.jsp
看一下报错信息
在这个MVC中,找不到ExampleService类的原因可能是类路径不正确或未正确引用所需的库。要确保ExampleService类的路径正确,并且在controller中正确地使用了类名。另外,ExampleService中的数据无法传达给ExampleView.jsp可能是因为您使用了错误的语法。在ExampleService类中,您应该使用request.setAttribute("date", format.format(date))将date的格式化字符串设置为date属性。在ExampleView.jsp中,您应该使用<%=request.getAttribute("date")%>来检索属性并将其呈现在页面上。
不知道你这个问题是否已经解决, 如果还没有解决的话:这里我创建数据库时,使用的是Navicat,大家使用Navicat将数据库创建成功之后,过几天在访问改数据库时,可能会弹出这样的窗口
出现这个问题的原因可能是:MySQL数据库的服务没有打开
打开之后就可以正常访问自己所创建的数据库了。
这里我是连接的MySQL数据库。如果大家想连接别的数据可,需要修改的地方:1.导入在项目中lib目录下的厂商驱动包要换成对应数据库的厂商驱动包。2.代码中的步骤1 、2要改成对应数据库的加载驱动和创建连接。
如果有啥错误的地方,欢迎大家指出来,有啥不懂的也可以在下方留言,我会及时回复的!!!欢迎大家批评指正!!!!