目录
一、前序工作 ........................................................................................................................... 2 二、Classes文件写一个Hello.java ......................................................................................... 2
写java文件 ...................................................................................................................... 2 配置web.xml文件 ........................................................................................................... 3 上网验证 ........................................................................................................................... 4 三、写登陆界面 ....................................................................................................................... 4
写java文件 ...................................................................................................................... 5 配置web.xml文件 ........................................................................................................... 7 上网验证 ........................................................................................................................... 7 四、同一用户的不同页面共享数据 ....................................................................................... 8
用sendRedirct()方法实现 ........................................................................................... 8 用隐藏表单实现 ............................................................................................................. 11 Session ............................................................................................................................. 11 五、Servlet链接数据库 ........................................................................................................ 13
建数据库的表 ................................................................................................................. 13 写java文件 .................................................................................................................... 13 放jar包 .......................................................................................................................... 16 Sql注入漏洞 ................................................................................................................... 16 六、Servlet中显示图片 ........................................................................................................ 18 七、分页技术 ......................................................................................................................... 19 八、cookie .............................................................................................................................. 24
使用cookie保存用户名,密码,在一定时间不用重复登陆 .................................... 24 删除cookie ..................................................................................................................... 29 九、网站框架 ......................................................................................................................... 30 十、ServletContext ................................................................................................................. 38
网站计数器 ..................................................................................................................... 39 优化 ................................................................................................................................. 40 用户ip地址 .................................................................................................................... 42 十一、界面美化 ..................................................................................................................... 42 十二、tomcat配置 ................................................................................................................ 59
如何修改tomcat端口 ................................................................................................... 59 如何设置虚拟目录 ......................................................................................................... 60 如何给tomcat的管理员设置密码 ............................................................................... 61 如何设置数据源和连接池 ............................................................................................. 62 附录......................................................................................................................................... 63
Jcreator不能导入javax.servlet包问题 ......................................................................... 63 本文数据库数据库 ......................................................................................................... 64 本文Web.xml文件配置 ................................................................................................ 64
一、前序工作
Tomcat安装好后
再webapps文件夹下新建mywebsite,里面包括WEB-INF,在里面是classes,lib和web.xml文件。
Jcreator配置好jdk路径后servlet也需要导入,configure—>options—>jdkprofile,选中jdk vension,点edit—>add?add achieve,选择tomcat里lib下的servlet-api,就ok了。详细看附录。
二、Classes文件写一个Hello.java 写java文件
有三种方法:
/*使用实现servlet接口的方式开发(不全)
package com.tsinghua; //一个自定义的包 import javax.servlet.*; import java.io.*;
import java.io.IoException;
public class Hello implements Servlet{
//该函数用于初始化servlet(类似与构造函数) //该函数只会被调用一次
public void init(ServletConfig parml) throws ServletException { System.out.println(\ }
public ServletConfig getServletConfig(){ return null; }
//这个函数用于处理业务逻辑
//程序员应该把业务逻辑代码写这里
//这个函数当用户每访问servlet时,都会被调用
//req:用于获得客户端(浏览器)信息 res:用于向客户端(浏览器)返回信息 public void service(ServletRequest req,ServletResponse res)throws ServletException{
System.out.println(\ //从res中得到printWriter PrintWriter pw=res.getWriter(); pw.println(\ }
public void destroy(){ } }
//方法二,继承GenericServlet(不全)
package com.tsinghua;
import javax.servlet.GenericServlet; import javax.servlet.*; import java.io.*;
public class HelloGenen extends GenericServlet{ //重写service方法
public void service(ServletRequest req,ServletResponse res){ //返回hello,world try{
PrintWriter pw=res.getWriter(); pw.println(\ }
catch (Exception ex){ ex.printStackTrace(); } } } */
//方法三,继承HttpServlet开发 package com.tsinghua; import javax.servlet.http.*; import java.io.*;
public class Hello extends HttpServlet{ //处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse res){ //业务逻辑 try {
PrintWriter pw = res.getWriter(); pw.println(\}
catch (Exception ex){ ex.printStackTrace(); } }
public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } }
配置web.xml文件
这个web.xml文件可以从旁边文件夹里拷来,主要改写的部分如下
上网验证
先打开apache-tomcat-8.5.8\\bin下的startup.bat,然后登陆下面地址,看manager里有没有mywebsite,若runing是true则ok
再登陆下面地址看看是否成功
显示了hello,成功了
三、写登陆界面