blog 从表单中获取内容(使用到servlet1)

1  如果我在一个工程当中引入了一些新的jar包,那么就要重启tomcat。
2  servlet会产生一个web.xml配置文件,
3  我们在使用servlet的时候用的是doPost方法,如果产生的doGet方法,那么调用doPost方法
4  在servlet当中可以使用request来获取参数
5  form最后的action注意要修改
6  request.setCharacterEncoding("utf-8");加上这一句,可以防止出现中文乱码。
7
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>BlogServlet</servlet-name>
    <servlet-class>cn.com.blog.BlogServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>BlogServlet</servlet-name>
    <url-pattern>/servlet/BlogServlet</url-pattern>
  </servlet-mapping>

一个servle主要的配置文件就是这些,如果使用post方法获取的话,也是获取这些内容。
使用的时候注意复制这一句:
<url-pattern>/servlet/BlogServlet</url-pattern>
但是写进去的时候注意写的只是servlet/BlogServlet。


addblog.jsp

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
.STYLE1 {font-size: 24px}
-->
</style>
</head>

<body>
<p class="STYLE1">请输入博文内容</p>
<form id="form1" name="form1" method="post" action="servlet/BlogServlet">
  <table width="653" height="349" border="0">
    <tr>
      <td width="175">主题:</td>
      <td width="468"><label>
        <input name="title" type="text" size="60" />
      </label></td>
    </tr>
    <tr>
      <td>类别:</td>
      <td><p>
        <label>
        <select name="category">
          <option value="2">心情故事</option>
          <option value="3">旅游故事</option>
        </select>
        </label>
        <br />
      </p></td>
    </tr>
    <tr>
      <td>内容:</td>
      <td><textarea name="content" cols="60" rows="15"></textarea>
      <label></label></td>
    </tr>
    <tr>
      <td><label></label></td>
      <td><label>
        <input type="submit" name="Submit" value="提交" />
        <input type="reset" name="Submit2" value="重置" />
      </label></td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
    </tr>
  </table>
</form>
<p>  </p>
</body>
</html>


java文件


package cn.com.blog;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BlogServlet extends HttpServlet {


	public BlogServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String category=request.getParameter("category");
		
		System.out.println(title);
		System.out.println(category);
		System.out.println(content);
		

	}

	public void init() throws ServletException {
		// Put your code here
	}

}


web.xml配置文件


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>BlogServlet</servlet-name>
    <servlet-class>cn.com.blog.BlogServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>BlogServlet</servlet-name>
    <url-pattern>/servlet/BlogServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值