当前位置: 技术问答>java相关
在线给分!提供servlet例子的都有分!
来源: 互联网 发布时间:2015-05-03
本文导语: 本人把java实现代码全都写在jsp页面里,有人说应该做成jsp页面只负责读表单的数据和显示功能,把功能实现代码都放到servlet中实现。觉得有一定道理,但从来没做过serlvet,急需您的指点,请问谁有这方面的例子?(...
本人把java实现代码全都写在jsp页面里,有人说应该做成jsp页面只负责读表单的数据和显示功能,把功能实现代码都放到servlet中实现。觉得有一定道理,但从来没做过serlvet,急需您的指点,请问谁有这方面的例子?(最好有servlet和jsp合作工作的实例)在线给分!
另外,tomcat3.2.4下怎么部署servlet?
另外,tomcat3.2.4下怎么部署servlet?
|
===================================
import java.io.*;
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
public class receiveservlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/plain");
ServletOutputStream out=res.getOutputStream();
out.print("receive user message:");
out.println(req.getParameter("message"));//得到上个页面的参数值message,应该显示test
}
public String getServletInfo()
{
return"returns a string representation of the current time";
}
}
|
去看看了:
http://www-900.ibm.com/developerWorks/java/j-struts/index.shtml
http://www-900.ibm.com/developerWorks/java/j-struts/index.shtml
|
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletForm extends HttpServlet
{public void doGet(HttpServletRequest req,HttpServletResponse res) throws javax.servlet.ServletException,IOException{
res.setContentType("text/html");
ServletOutputStream httpOutStream=res.getOutputStream();
httpOutStream.println("实例二 ServletForm.java");
httpOutStream.println("");
httpOutStream.println("表单处理的Servlet实例");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("Name");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("Address");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("City");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("State");
httpOutStream.println("");
httpOutStream.println("湖南省广东省北京市上海市陕西省");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("Zip");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("Phone");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("家庭电话办公室电话寻呼机手机");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println(" ");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("Servlet Name" + req.getServerName() +"");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("Servlet Path"+ req.getServletPath()+"");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("Servlet Information"+getServletInfo()+"");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.close();
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws javax.servlet.ServletException,IOException{
Enumeration formFields;
String fieldName;
res.setContentType("text/html");
ServletOutputStream httpOutStream=res.getOutputStream();
httpOutStream.println("实例二ServletForm.java");
httpOutStream.println("");
httpOutStream.println("Forms Processing Servlet");
httpOutStream.println("Data POSTed from the Client Browser");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("ParameterValue");
httpOutStream.println("");
httpOutStream.println("QueryString");
httpOutStream.println(req.getQueryString()+"");
httpOutStream.println("Content Length");
httpOutStream.println(req.getContentLength()+"");
formFields=req.getParameterNames();
while(formFields.hasMoreElements()){
httpOutStream.println("");
httpOutStream.println("");
fieldName=formFields.nextElement().toString();
httpOutStream.println("fieldName");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println(req.getParameterValues(fieldName)[0]);
httpOutStream.println("");
httpOutStream.println("");}
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.println("");
httpOutStream.close();
}
public String getServletInfo(){
return "实例二ServletForm.java.Forms processing example";
}
}
|
http://www.51jsp.net/InfoView/Info.asp?ID=928
一个完整的例子
一个完整的例子
|
你在JSP里面提交都到一个Servlet,在Servlet里处理业务逻辑,然后转到JSP显示
下载最新的tomcat4.0.3,只改端口,设置CLASSPATH,就可以支持Servlet
下载最新的tomcat4.0.3,只改端口,设置CLASSPATH,就可以支持Servlet
|
你最好找本书或者资料试一下
如果有问题的话再贴出来大家给你解决
如果有问题的话再贴出来大家给你解决