Integrate, Update Twitter using JSP
by Vinoth[ Edit ] 2009-07-30 17:26:58
Hi,
The bellow code will help you to update your twitter message using JSP.
< %@ page import="sun.misc.BASE64Encoder,java.io.InputStreamReader,java.io.BufferedReader"% >
< %@ page import="java.net.URLConnection,java.net.URL,java.net.URLEncoder,java.io.OutputStreamWriter"% >
< %
String user=request.getParameter("user");
String passwd=request.getParameter("passwd");
String msg=request.getParameter("msg");
String output="";
if(user!=null&&passwd!=null&&msg!=null){
URL url = new URL("https://twitter.com/statuses/update.xml");
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
String authorization = user + ":" + passwd;
BASE64Encoder encoder = new BASE64Encoder();
String encoded = new String
(encoder.encodeBuffer(authorization.getBytes())).trim();
connection.setRequestProperty("Authorization", "Basic " + encoded);
OutputStreamWriter outs = new OutputStreamWriter(
connection.getOutputStream());
outs.write("status=" + URLEncoder.encode(msg, "UTF-8"));
outs.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String ln="";
while ((ln = in.readLine()) != null) {
output+=ln;
}
in.close();
}
% >
< body >
< table align="center" border="0" width="75%" >
< tr >
< td align="center" valign="bottom" > < font class="msg" > < %=output% > ; < /font > < /td>
< /tr >
< tr >
< td >
< form name="feed" METHOD="POST" onsubmit="return validate();" >
< table style="border: 2px rgb(204, 204, 204); padding: 10px 0px; background-color: rgb(252, 253, 254); font-family: Verdana; font-size: 12px;" align="center" border="0" cellpadding="7" cellspacing="5" width="60%" >
< tr >
< td colspan="2" class="headline" align="center" > < hr style="color:#cccccc" > Twitter Integration < hr style="color:#cccccc" > < /td >
</tr >
<tr > <td style="font-family: arial; font-size: 13px; font-weight: bold;" > User Name : < /td > < td > < input type="text" name="user" size=45 maxlength="100" > < /td > < /tr >
< tr > < td style="font-family: arial; font-size: 13px; font-weight: bold;" > Password : < /td > < td > < input type="password" name="passwd" size=45 maxlength="100" > < /td > < /tr >
< tr > < td colspan=2 style="font-family: arial; font-size: 13px; font-weight: bold;" >
Content:
< br > < textarea name="msg" rows=8 cols=57 wrap=physical > < /textarea > < /td >
< /tr >
< tr > < td colspan=2 align=right > < input name="submit" type="submit" value="Send" style="background-color: #E6EBEF; font-family: arial; font-size: 12px; font-weight: bold;" > < /td > < /tr >
< /table >
< /form >
< /td >
< /tr >
< /table >
< /body >
< /html >