<html>
<head>
<title> Remove Line Numbers </title>
<style type="text/css">
html, body, textarea { height: 100%; width: 100%; margin: 0px; padding: 0px; border: 0px; }
textarea { width: 100%; height: 100%; background: none ; line-height: 20px; font-size: 15px; resize: none; outline:none; }
</style>
<script type="text/javascript">
var regex = new RegExp("^\\s*\\d+\\.?", "gm");
function removeLineNumber(textArea) {
textArea.value = textArea.value.replace(regex, "");
}
</script>
</head>
<body onload="document.getElementById('textarea').focus()">
<textarea id="textarea" onkeyup="removeLineNumber(this)" rows="50" cols="250">Paste your Code Here.</textarea>
</body>
</html>
Friday, 30 March 2012
Remove Line Numbers
Monday, 26 March 2012
Java Web Service
This tutorial is for windows Users.
D:\>md jcode
D:\jcode>md ws-server
D:\jcode\ws-server>notepad HelloWorldWS.java
Paste the Below Code in notepad and Save.
package org.example.webservice;
import javax.jws.*;
import javax.xml.ws.Endpoint;
@WebService
public class HelloWorldWS{
public String echo(String name) {
return "HI ! ,"+name;
}
public static void main(String args[]){
Endpoint.publish("http://localhost:8080/HelloWorldWS",new HelloWorldWS());
}
}
The url along with the port can be changed to include ur ip address, so that it can be remotely accessed.
D:\jcode\ws-server>javac HelloWorldWS.java
D:\jcode\ws-server>md org
D:\jcode\ws-server>md org\exampleD:\jcode>md org\example\webservice
D:\jcode\ws-server>copy HelloWorldWS.class org\example\webservice
D:\jcode\ws-server>java org.example.webservice.HelloWorldWS
Keep this running. Now open another cmd window.
Also you can Check the WSDL from your browser, which will be consumed by the client, with the following link.
http://localhost:8080/HelloWorldWS?wsdl
D:\jcode\ws-server>cd ..
D:\jcode>md wsclient
D:\jcode>wsimport -s wsclient http://localhost:8080/HelloWorldWS?wsdl
D:\jcode>notepad wsclient\WSClient.java
Paste the Below Code in notepad and Save.
package wsclient;
import org.example.webservice.*;
public class WSClient {
public static void main(String args[]){
HelloWorldWS obj = (new HelloWorldWSService()).getHelloWorldWSPort();
System.out.println(obj.echo(" World "));
}
}
D:\jcode>javac wsclient/WSClient.java
D:\jcode>java -Dcom.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.
dump=true wsclient.WSClient
---[HTTP request - http://localhost:8080/HelloWorldWS]---
Content-type: text/xml;charset="utf-8"
Soapaction: ""
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2,
*/*; q=.2
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envel
ope/"><S:Body><ns2:echo xmlns:ns2="http://webservice.example.org/"><arg0> World
</arg0></ns2:echo></S:Body></S:Envelope>--------------------
---[HTTP response - http://localhost:8080 /HelloWorldWS - 200]---
Transfer-encoding: chunked
null: HTTP/1.1 200 OK
Content-type: text/xml;charset="utf-8"
<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envel
ope/"><S:Body><ns2:echoResponse xmlns:ns2="http://webservice.example.org/"><retu
rn>HI ! , World </return></ns2:echoResponse></S:Body></S:Envelope>--------------
------
HI ! , World
SOAP REQUEST GENERATED :
<?xml version="1.0" ?>
<S:Envelope
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:echo xmlns:ns2="http://webservice.example.org/">
<arg0>World</arg0>
</ns2:echo>
</S:Body>
</S:Envelope>
SOAP RESPONSE GENERATED :
<?xml version="1.0"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:echoResponse xmlns:ns2="http://webservice.example.org/">
<return>HI ! ,World</return>
</ns2:echoResponse>
</S:Body>
</S:Envelope>
Tuesday, 3 January 2012
Thread Race in JAVA
1. Start Command Line on Windows.
cmd>path
cmd>java -version
2.Type the following command in command line(Windows).
cmd>copy con RaceCourse.java
3.Copy the following code and Paste into cmd. Press Ctrl+Z and your File gets Saved.
javac -d classes RaceCourse.java
cmd>copy con ThreadRace.java
[paste your your code here]
[Press Ctrl+Z]
cmd>java -classpath classes ThreadRace
- Open cmd(Windows) or terminal(Unix)
- Set Environment Variable JDK_HOME and JRE_HOME on Windows with jdk bin directory(eg. C:\Program Files\Java\jdk1.6.24\bin).
- Set path to add these to two variables
cmd>path
- check javac and java commands in the command Line.
- Try the following two commands in cmd.
cmd>java -version
- If the versions are different the reconfigure your paths
2.Type the following command in command line(Windows).
cmd>copy con RaceCourse.java
3.Copy the following code and Paste into cmd. Press Ctrl+Z and your File gets Saved.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
//THE RACE COURSE TO RUN ON
public class RaceCourse extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
int threadLength1, threadLength2, threadLength3;
private static final int START_X = 50;
private static final int START_Y = 50;
private static final int INC = 1;
private static final int THREAD_WIDTH = 30;
private static final int THREAD_SEPERATION = 90;
boolean thread1 = false, thread2 = false;
public RaceCourse() {
threadLength1 = threadLength2 = threadLength3 = INC;
setSize(getPreferredSize());
RaceCourse.this.setBackground(Color.white);
}
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if (thread1) {
g2d.setColor(new Color(200, 20, 20, 255));
g2d.fillRect(START_X, START_Y, threadLength1, THREAD_WIDTH);
}
else if (thread2) {
g2d.setColor(new Color(20, 200, 20, 255));
g2d.fillRect(START_X, START_Y + THREAD_SEPERATION, threadLength2, THREAD_WIDTH);
} else {
g2d.setColor(new Color(20, 20, 200, 255));
g2d.fillRect(START_X, START_Y + 2 * THREAD_SEPERATION, threadLength3, THREAD_WIDTH);
}
}
// A comman run method shared by three Threads.
public void run() {
while (true)
try {
Thread.sleep(100);
System.out.println("Current Thread:"
+ Thread.currentThread().getName());
if (Thread.currentThread().getName().equals("thread1")) {
thread1 = true;
thread2 = false;
threadLength1 = threadLength1 + INC;
repaint(START_X, START_Y, threadLength1, THREAD_WIDTH);
// this.wait();
} else if (Thread.currentThread().getName().equals("thread2")) {
Thread.sleep(100);
// Thread.yield();
thread2 = true;
thread1 = false;
threadLength2 = threadLength2 + INC;
repaint(START_X, START_Y + THREAD_SEPERATION, threadLength2, THREAD_WIDTH);
// this.notify();
} else {
Thread.sleep(400);
thread2 = false;
thread1 = false;
threadLength3 = threadLength3 + INC;
repaint(START_X, START_Y + 2 * THREAD_SEPERATION, threadLength3, THREAD_WIDTH);
// this.wait();
}
}
catch (IllegalMonitorStateException is) {
System.err.println("Error:" + is);
} catch (InterruptedException ie) {
System.err.println("Error:" + ie);
}
}
}
4.Compile the above Codejavac -d classes RaceCourse.java
- Above command compiles the java file and stores into classes folder if it exists(create classes folder next to RaceCourse.java file if it doesn't exist)
- If it compiles successfully without any errors go to next step.
cmd>copy con ThreadRace.java
[paste your your code here]
[Press Ctrl+Z]
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class ThreadRace extends JFrame {
private static final long serialVersionUID = 1L;
RaceCourse canvas;
public ThreadRace() {
super(" Race Course ");
canvas = new RaceCourse();
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new JScrollPane(canvas));
setVisible(true);
pack();
}
public static void main(String[] args) {
ThreadRace course = new ThreadRace();
Thread thread1 = new Thread(course.canvas, "thread1");
Thread thread2 = new Thread(course.canvas, "thread2");
Thread thread3 = new Thread(course.canvas, "thread3");
thread1.setPriority(Thread.MAX_PRIORITY);
thread2.setPriority(Thread.NORM_PRIORITY);
thread3.setPriority(Thread.MIN_PRIORITY);
thread1.start();
thread2.start();
thread3.start();
}
}
6.Compile ThreadRace.java as shown in step 4- Check if ThreadRace.class and RaceCourse.class file is located in classes folder
cmd>java -classpath classes ThreadRace
Wednesday, 21 December 2011
Set up LAMP Server at home
1. Follow Ubuntu's Help to Install LAMP Server
https://help.ubuntu.com/community/ApacheMySQLPHP
2. Create a simple PHP file in /var/www/
7.Create a Form in HTML.
8. Now Write a PHP script to process the Student Form.
http://127.0.0.1/insertform.html
http://127.0.0.1/dbinsert.php
http://127.0.0.1/dbresult.php
https://help.ubuntu.com/community/ApacheMySQLPHP
2. Create a simple PHP file in /var/www/
- Open a Terminal.
- sudo gedit /var/www/info.php
- Type your Password. Press Enter.
- Paste the Following Code into opened File(/var/www/info.php).
<?php echo "Hey There , This is Working." phpinfo(); ?>
- Save the File.
- sudo service apache2 restart
- http://localhost/info.php
- http://127.0.0.1/info.php
- http://<ur IP address>/info.php
- http://localhost/info.php
- Open a Terminal
- admin@mooshtoo:~$ mysql -u root -p
- Enter password:
- Copy the Following Commands in the Terminal:
create database record; create table record.student( rno int, name varchar(40), phy int, chem int, math int );
7.Create a Form in HTML.
- sudo gedit /var/www/StudentForm.html
- Enter Password:
- Paste the Following Code in StudentForm.html and Save.
<HTML>
<Head>
<title>Insert Student Record</title>
<style type="text/css">
table{
border:1;
border-collapse:collapse;
font: normal 12px 'Lucida Grande',Verdana,sans-serif;
}
td{
color:#663333;font-family:verdana;
border-bottom: 1px solid #666;
padding-left:10px;
background-color:#F0F8FF;
}
#sub{ text-align:center;}
</style>
</Head>
<body>
<h2> Student Record Form </h2>
<form action="dbinsert.php" method="POST" id="insert">
<table>
<tr>
<td>Roll Number</td>
<td><input type="text" size=40 name="rno"></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" size=40 name="name"></td>
</tr>
<tr>
<td >Physics</td>
<td><textarea name="phy" cols=3 rows=1></textarea> </td>
</tr>
<tr>
<td>Chemistry</td>
<td><textarea name="chem" cols=3 rows=1></textarea> </td>
</tr>
<tr>
<td>Mathematics</td>
<td><textarea name="math" cols=3 rows=1></textarea> </td>
</tr>
<tr>
<td colspan=2 id="sub"><input type="submit" name="submit" value="submit" ></td>
</tr>
</Table>
</form>
</BODY>
</Html>
8. Now Write a PHP script to process the Student Form.
- Create a new file /var/www/dbinsert.php
- Paste the following code in it.
<?php
$con = mysql_connect("localhost","root","<Your password>");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("record", $con);
$result = mysql_query("SELECT * FROM record.student");
//Get data in local variable
$v_rno=$_POST['rno'];
$v_name=$_POST['name'];
$v_phy=$_POST['phy'];
$v_chem=$_POST['chem'];
$v_math=$_POST['math'];
// check for null values
if ($v_rno=="" or $v_name=="")
echo "All fields must be entered, hit back button and re-enter information";
else{
$query="insert into record.student values($v_rno,'$v_name',$v_phy,$v_chem,$v_math)";
mysql_query($query) or die(mysql_error());
echo "Your message has been received";
}
?>
<br><br>
<!-- For Navigation -->
<a href='insertform.html'>Insert Another Record</a>
<a href='dbresult.php'>Student Record</a>
9.Write another PHP script to Display Student Records.- Create a new file /var/www/dbresult.php
- Paste the following code in it.
<?php
$con = mysql_connect("localhost","root","<Your password>");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("record", $con);
$result = mysql_query("SELECT * FROM record.student");
?>
<table border='1'>
<tr>
<th>Roll No.</th>
<th>Name</th>
<th>Physics</th>
<th>Chemistry</th>
<th>Mathematics</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rno'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['phy'] . "</td>";
echo "<td>" . $row['chem'] . "</td>";
echo "<td>" . $row['math'] . "</td>";
echo "</tr>";
}
mysql_close($con);
?>
</table>
<br><br>
<!-- For Navigation -->
<a href='insertform.html' alt='New%20Student%20Record' >Insert Another Record</a>
10. Now test the following links in your browser.http://127.0.0.1/insertform.html
http://127.0.0.1/dbinsert.php
http://127.0.0.1/dbresult.php
Subscribe to:
Posts (Atom)